2 * drivers/w1/masters/omap_hdq.c
4 * Copyright (C) 2007 Texas Instruments, Inc.
6 * This file is licensed under the terms of the GNU General Public License
7 * version 2. This program is licensed "as is" without any warranty of any
8 * kind, whether express or implied.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/interrupt.h>
15 #include <linux/err.h>
16 #include <linux/clk.h>
20 #include <mach/hardware.h>
23 #include "../w1_int.h"
25 #define MOD_NAME "OMAP_HDQ:"
27 #define OMAP_HDQ_REVISION 0x00
28 #define OMAP_HDQ_TX_DATA 0x04
29 #define OMAP_HDQ_RX_DATA 0x08
30 #define OMAP_HDQ_CTRL_STATUS 0x0c
31 #define OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK (1<<6)
32 #define OMAP_HDQ_CTRL_STATUS_CLOCKENABLE (1<<5)
33 #define OMAP_HDQ_CTRL_STATUS_GO (1<<4)
34 #define OMAP_HDQ_CTRL_STATUS_INITIALIZATION (1<<2)
35 #define OMAP_HDQ_CTRL_STATUS_DIR (1<<1)
36 #define OMAP_HDQ_CTRL_STATUS_MODE (1<<0)
37 #define OMAP_HDQ_INT_STATUS 0x10
38 #define OMAP_HDQ_INT_STATUS_TXCOMPLETE (1<<2)
39 #define OMAP_HDQ_INT_STATUS_RXCOMPLETE (1<<1)
40 #define OMAP_HDQ_INT_STATUS_TIMEOUT (1<<0)
41 #define OMAP_HDQ_SYSCONFIG 0x14
42 #define OMAP_HDQ_SYSCONFIG_SOFTRESET (1<<1)
43 #define OMAP_HDQ_SYSCONFIG_AUTOIDLE (1<<0)
44 #define OMAP_HDQ_SYSSTATUS 0x18
45 #define OMAP_HDQ_SYSSTATUS_RESETDONE (1<<0)
47 #define OMAP_HDQ_FLAG_CLEAR 0
48 #define OMAP_HDQ_FLAG_SET 1
49 #define OMAP_HDQ_TIMEOUT (HZ/5)
51 #define OMAP_HDQ_MAX_USER 4
53 static DECLARE_WAIT_QUEUE_HEAD(hdq_wait_queue);
58 void __iomem *hdq_base;
59 /* lock status update */
60 struct mutex hdq_mutex;
66 spinlock_t hdq_spinlock;
68 * Used to control the call to omap_hdq_get and omap_hdq_put.
69 * HDQ Protocol: Write the CMD|REG_address first, followed by
70 * the data wrire or read.
75 static int __init omap_hdq_probe(struct platform_device *pdev);
76 static int omap_hdq_remove(struct platform_device *pdev);
78 static struct platform_driver omap_hdq_driver = {
79 .probe = omap_hdq_probe,
80 .remove = omap_hdq_remove,
86 static u8 omap_w1_read_byte(void *_hdq);
87 static void omap_w1_write_byte(void *_hdq, u8 byte);
88 static u8 omap_w1_reset_bus(void *_hdq);
89 static void omap_w1_search_bus(void *_hdq, u8 search_type,
90 w1_slave_found_callback slave_found);
93 static struct w1_bus_master omap_w1_master = {
94 .read_byte = omap_w1_read_byte,
95 .write_byte = omap_w1_write_byte,
96 .reset_bus = omap_w1_reset_bus,
97 .search = omap_w1_search_bus,
100 /* HDQ register I/O routines */
101 static inline u8 hdq_reg_in(struct hdq_data *hdq_data, u32 offset)
103 return __raw_readb(hdq_data->hdq_base + offset);
106 static inline void hdq_reg_out(struct hdq_data *hdq_data, u32 offset, u8 val)
108 __raw_writeb(val, hdq_data->hdq_base + offset);
111 static inline u8 hdq_reg_merge(struct hdq_data *hdq_data, u32 offset,
114 u8 new_val = (__raw_readb(hdq_data->hdq_base + offset) & ~mask)
116 __raw_writeb(new_val, hdq_data->hdq_base + offset);
122 * Wait for one or more bits in flag change.
123 * HDQ_FLAG_SET: wait until any bit in the flag is set.
124 * HDQ_FLAG_CLEAR: wait until all bits in the flag are cleared.
125 * return 0 on success and -ETIMEDOUT in the case of timeout.
127 static int hdq_wait_for_flag(struct hdq_data *hdq_data, u32 offset,
128 u8 flag, u8 flag_set, u8 *status)
131 unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT;
133 if (flag_set == OMAP_HDQ_FLAG_CLEAR) {
134 /* wait for the flag clear */
135 while (((*status = hdq_reg_in(hdq_data, offset)) & flag)
136 && time_before(jiffies, timeout)) {
137 schedule_timeout_uninterruptible(1);
141 } else if (flag_set == OMAP_HDQ_FLAG_SET) {
142 /* wait for the flag set */
143 while (!((*status = hdq_reg_in(hdq_data, offset)) & flag)
144 && time_before(jiffies, timeout)) {
145 schedule_timeout_uninterruptible(1);
147 if (!(*status & flag))
155 /* write out a byte and fill *status with HDQ_INT_STATUS */
156 static int hdq_write_byte(struct hdq_data *hdq_data, u8 val, u8 *status)
160 unsigned long irqflags;
164 spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
165 /* clear interrupt flags via a dummy read */
166 hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
167 /* ISR loads it with new INT_STATUS */
168 hdq_data->hdq_irqstatus = 0;
169 spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
171 hdq_reg_out(hdq_data, OMAP_HDQ_TX_DATA, val);
174 hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, OMAP_HDQ_CTRL_STATUS_GO,
175 OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
176 /* wait for the TXCOMPLETE bit */
177 ret = wait_event_timeout(hdq_wait_queue,
178 hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT);
180 dev_dbg(hdq_data->dev, "TX wait elapsed\n");
184 *status = hdq_data->hdq_irqstatus;
185 /* check irqstatus */
186 if (!(*status & OMAP_HDQ_INT_STATUS_TXCOMPLETE)) {
187 dev_dbg(hdq_data->dev, "timeout waiting for"
188 "TXCOMPLETE/RXCOMPLETE, %x", *status);
193 /* wait for the GO bit return to zero */
194 ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
195 OMAP_HDQ_CTRL_STATUS_GO,
196 OMAP_HDQ_FLAG_CLEAR, &tmp_status);
198 dev_dbg(hdq_data->dev, "timeout waiting GO bit"
199 "return to zero, %x", tmp_status);
206 /* HDQ Interrupt service routine */
207 static irqreturn_t hdq_isr(int irq, void *_hdq)
209 struct hdq_data *hdq_data = _hdq;
210 unsigned long irqflags;
212 spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
213 hdq_data->hdq_irqstatus = hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
214 spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
215 dev_dbg(hdq_data->dev, "hdq_isr: %x", hdq_data->hdq_irqstatus);
217 if (hdq_data->hdq_irqstatus &
218 (OMAP_HDQ_INT_STATUS_TXCOMPLETE | OMAP_HDQ_INT_STATUS_RXCOMPLETE
219 | OMAP_HDQ_INT_STATUS_TIMEOUT)) {
220 /* wake up sleeping process */
221 wake_up(&hdq_wait_queue);
227 /* HDQ Mode: always return success */
228 static u8 omap_w1_reset_bus(void *_hdq)
233 /* W1 search callback function */
234 static void omap_w1_search_bus(void *_hdq, u8 search_type,
235 w1_slave_found_callback slave_found)
237 u64 module_id, rn_le, cs, id;
244 rn_le = cpu_to_le64(module_id);
246 * HDQ might not obey truly the 1-wire spec.
247 * So calculate CRC based on module parameter.
249 cs = w1_calc_crc8((u8 *)&rn_le, 7);
250 id = (cs << 56) | module_id;
252 slave_found(_hdq, id);
255 static int _omap_hdq_reset(struct hdq_data *hdq_data)
260 hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG, OMAP_HDQ_SYSCONFIG_SOFTRESET);
262 * Select HDQ mode & enable clocks.
263 * It is observed that INT flags can't be cleared via a read and GO/INIT
264 * won't return to zero if interrupt is disabled. So we always enable
267 hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
268 OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
269 OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
271 /* wait for reset to complete */
272 ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_SYSSTATUS,
273 OMAP_HDQ_SYSSTATUS_RESETDONE, OMAP_HDQ_FLAG_SET, &tmp_status);
275 dev_dbg(hdq_data->dev, "timeout waiting HDQ reset, %x",
278 hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
279 OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
280 OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
281 hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
282 OMAP_HDQ_SYSCONFIG_AUTOIDLE);
288 /* Issue break pulse to the device */
289 static int omap_hdq_break(struct hdq_data *hdq_data)
293 unsigned long irqflags;
295 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
297 dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
302 spin_lock_irqsave(&hdq_data->hdq_spinlock, irqflags);
303 /* clear interrupt flags via a dummy read */
304 hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
305 /* ISR loads it with new INT_STATUS */
306 hdq_data->hdq_irqstatus = 0;
307 spin_unlock_irqrestore(&hdq_data->hdq_spinlock, irqflags);
309 /* set the INIT and GO bit */
310 hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
311 OMAP_HDQ_CTRL_STATUS_INITIALIZATION | OMAP_HDQ_CTRL_STATUS_GO,
312 OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
313 OMAP_HDQ_CTRL_STATUS_GO);
315 /* wait for the TIMEOUT bit */
316 ret = wait_event_timeout(hdq_wait_queue,
317 hdq_data->hdq_irqstatus, OMAP_HDQ_TIMEOUT);
319 dev_dbg(hdq_data->dev, "break wait elapsed\n");
324 tmp_status = hdq_data->hdq_irqstatus;
325 /* check irqstatus */
326 if (!(tmp_status & OMAP_HDQ_INT_STATUS_TIMEOUT)) {
327 dev_dbg(hdq_data->dev, "timeout waiting for TIMEOUT, %x",
333 * wait for both INIT and GO bits rerurn to zero.
334 * zero wait time expected for interrupt mode.
336 ret = hdq_wait_for_flag(hdq_data, OMAP_HDQ_CTRL_STATUS,
337 OMAP_HDQ_CTRL_STATUS_INITIALIZATION |
338 OMAP_HDQ_CTRL_STATUS_GO, OMAP_HDQ_FLAG_CLEAR,
341 dev_dbg(hdq_data->dev, "timeout waiting INIT&GO bits"
342 "return to zero, %x", tmp_status);
345 mutex_unlock(&hdq_data->hdq_mutex);
350 static int hdq_read_byte(struct hdq_data *hdq_data, u8 *val)
354 unsigned long timeout = jiffies + OMAP_HDQ_TIMEOUT;
356 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
362 if (!hdq_data->hdq_usecount) {
367 if (!(hdq_data->hdq_irqstatus & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
368 hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS,
369 OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO,
370 OMAP_HDQ_CTRL_STATUS_DIR | OMAP_HDQ_CTRL_STATUS_GO);
372 * The RX comes immediately after TX. It
373 * triggers another interrupt before we
374 * sleep. So we have to wait for RXCOMPLETE bit.
376 while (!(hdq_data->hdq_irqstatus
377 & OMAP_HDQ_INT_STATUS_RXCOMPLETE)
378 && time_before(jiffies, timeout)) {
379 schedule_timeout_uninterruptible(1);
381 hdq_reg_merge(hdq_data, OMAP_HDQ_CTRL_STATUS, 0,
382 OMAP_HDQ_CTRL_STATUS_DIR);
383 status = hdq_data->hdq_irqstatus;
384 /* check irqstatus */
385 if (!(status & OMAP_HDQ_INT_STATUS_RXCOMPLETE)) {
386 dev_dbg(hdq_data->dev, "timeout waiting for"
387 "RXCOMPLETE, %x", status);
392 /* the data is ready. Read it in! */
393 *val = hdq_reg_in(hdq_data, OMAP_HDQ_RX_DATA);
395 mutex_unlock(&hdq_data->hdq_mutex);
401 /* Enable clocks and set the controller to HDQ mode */
402 static int omap_hdq_get(struct hdq_data *hdq_data)
406 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
412 if (OMAP_HDQ_MAX_USER == hdq_data->hdq_usecount) {
413 dev_dbg(hdq_data->dev, "attempt to exceed the max use count");
417 hdq_data->hdq_usecount++;
418 try_module_get(THIS_MODULE);
419 if (1 == hdq_data->hdq_usecount) {
420 if (clk_enable(hdq_data->hdq_ick)) {
421 dev_dbg(hdq_data->dev, "Can not enable ick\n");
425 if (clk_enable(hdq_data->hdq_fck)) {
426 dev_dbg(hdq_data->dev, "Can not enable fck\n");
427 clk_disable(hdq_data->hdq_ick);
432 /* make sure HDQ is out of reset */
433 if (!(hdq_reg_in(hdq_data, OMAP_HDQ_SYSSTATUS) &
434 OMAP_HDQ_SYSSTATUS_RESETDONE)) {
435 ret = _omap_hdq_reset(hdq_data);
437 /* back up the count */
438 hdq_data->hdq_usecount--;
440 /* select HDQ mode & enable clocks */
441 hdq_reg_out(hdq_data, OMAP_HDQ_CTRL_STATUS,
442 OMAP_HDQ_CTRL_STATUS_CLOCKENABLE |
443 OMAP_HDQ_CTRL_STATUS_INTERRUPTMASK);
444 hdq_reg_out(hdq_data, OMAP_HDQ_SYSCONFIG,
445 OMAP_HDQ_SYSCONFIG_AUTOIDLE);
446 hdq_reg_in(hdq_data, OMAP_HDQ_INT_STATUS);
452 clk_put(hdq_data->hdq_ick);
453 clk_put(hdq_data->hdq_fck);
455 mutex_unlock(&hdq_data->hdq_mutex);
460 /* Disable clocks to the module */
461 static int omap_hdq_put(struct hdq_data *hdq_data)
465 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
469 if (0 == hdq_data->hdq_usecount) {
470 dev_dbg(hdq_data->dev, "attempt to decrement use count"
474 hdq_data->hdq_usecount--;
475 module_put(THIS_MODULE);
476 if (0 == hdq_data->hdq_usecount) {
477 clk_disable(hdq_data->hdq_ick);
478 clk_disable(hdq_data->hdq_fck);
481 mutex_unlock(&hdq_data->hdq_mutex);
486 /* Read a byte of data from the device */
487 static u8 omap_w1_read_byte(void *_hdq)
489 struct hdq_data *hdq_data = _hdq;
493 ret = hdq_read_byte(hdq_data, &val);
495 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
497 dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
500 hdq_data->init_trans = 0;
501 mutex_unlock(&hdq_data->hdq_mutex);
502 omap_hdq_put(hdq_data);
506 /* Write followed by a read, release the module */
507 if (hdq_data->init_trans) {
508 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
510 dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
513 hdq_data->init_trans = 0;
514 mutex_unlock(&hdq_data->hdq_mutex);
515 omap_hdq_put(hdq_data);
521 /* Write a byte of data to the device */
522 static void omap_w1_write_byte(void *_hdq, u8 byte)
524 struct hdq_data *hdq_data = _hdq;
528 /* First write to initialize the transfer */
529 if (hdq_data->init_trans == 0)
530 omap_hdq_get(hdq_data);
532 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
534 dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
537 hdq_data->init_trans++;
538 mutex_unlock(&hdq_data->hdq_mutex);
540 ret = hdq_write_byte(hdq_data, byte, &status);
542 dev_dbg(hdq_data->dev, "TX failure:Ctrl status %x\n", status);
546 /* Second write, data transfered. Release the module */
547 if (hdq_data->init_trans > 1) {
548 omap_hdq_put(hdq_data);
549 ret = mutex_lock_interruptible(&hdq_data->hdq_mutex);
551 dev_dbg(hdq_data->dev, "Could not acquire mutex\n");
554 hdq_data->init_trans = 0;
555 mutex_unlock(&hdq_data->hdq_mutex);
561 static int __init omap_hdq_probe(struct platform_device *pdev)
563 struct hdq_data *hdq_data;
564 struct resource *res;
568 hdq_data = kmalloc(sizeof(*hdq_data), GFP_KERNEL);
570 dev_dbg(&pdev->dev, "unable to allocate memory\n");
575 hdq_data->dev = &pdev->dev;
576 platform_set_drvdata(pdev, hdq_data);
578 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
580 dev_dbg(&pdev->dev, "unable to get resource\n");
585 hdq_data->hdq_base = ioremap(res->start, SZ_4K);
586 if (!hdq_data->hdq_base) {
587 dev_dbg(&pdev->dev, "ioremap failed\n");
592 /* get interface & functional clock objects */
593 hdq_data->hdq_ick = clk_get(&pdev->dev, "hdq_ick");
594 hdq_data->hdq_fck = clk_get(&pdev->dev, "hdq_fck");
596 if (IS_ERR(hdq_data->hdq_ick) || IS_ERR(hdq_data->hdq_fck)) {
597 dev_dbg(&pdev->dev, "Can't get HDQ clock objects\n");
598 if (IS_ERR(hdq_data->hdq_ick)) {
599 ret = PTR_ERR(hdq_data->hdq_ick);
602 if (IS_ERR(hdq_data->hdq_fck)) {
603 ret = PTR_ERR(hdq_data->hdq_fck);
604 clk_put(hdq_data->hdq_ick);
609 hdq_data->hdq_usecount = 0;
610 mutex_init(&hdq_data->hdq_mutex);
612 if (clk_enable(hdq_data->hdq_ick)) {
613 dev_dbg(&pdev->dev, "Can not enable ick\n");
618 if (clk_enable(hdq_data->hdq_fck)) {
619 dev_dbg(&pdev->dev, "Can not enable fck\n");
624 rev = hdq_reg_in(hdq_data, OMAP_HDQ_REVISION);
625 dev_info(&pdev->dev, "OMAP HDQ Hardware Rev %c.%c. Driver in %s mode\n",
626 (rev >> 4) + '0', (rev & 0x0f) + '0', "Interrupt");
628 spin_lock_init(&hdq_data->hdq_spinlock);
630 irq = platform_get_irq(pdev, 0);
636 ret = request_irq(irq, hdq_isr, IRQF_DISABLED, "omap_hdq", hdq_data);
638 dev_dbg(&pdev->dev, "could not request irq\n");
642 omap_hdq_break(hdq_data);
644 /* don't clock the HDQ until it is needed */
645 clk_disable(hdq_data->hdq_ick);
646 clk_disable(hdq_data->hdq_fck);
648 omap_w1_master.data = hdq_data;
650 ret = w1_add_master_device(&omap_w1_master);
652 dev_dbg(&pdev->dev, "Failure in registering w1 master\n");
660 clk_disable(hdq_data->hdq_fck);
663 clk_disable(hdq_data->hdq_ick);
666 clk_put(hdq_data->hdq_ick);
667 clk_put(hdq_data->hdq_fck);
670 iounmap(hdq_data->hdq_base);
674 platform_set_drvdata(pdev, NULL);
682 static int omap_hdq_remove(struct platform_device *pdev)
684 struct hdq_data *hdq_data = platform_get_drvdata(pdev);
686 mutex_lock(&hdq_data->hdq_mutex);
688 if (hdq_data->hdq_usecount) {
689 dev_dbg(&pdev->dev, "removed when use count is not zero\n");
693 mutex_unlock(&hdq_data->hdq_mutex);
695 /* remove module dependency */
696 clk_put(hdq_data->hdq_ick);
697 clk_put(hdq_data->hdq_fck);
698 free_irq(INT_24XX_HDQ_IRQ, hdq_data);
699 platform_set_drvdata(pdev, NULL);
700 iounmap(hdq_data->hdq_base);
709 return platform_driver_register(&omap_hdq_driver);
711 module_init(omap_hdq_init);
716 platform_driver_unregister(&omap_hdq_driver);
718 module_exit(omap_hdq_exit);
720 module_param(w1_id, int, S_IRUSR);
721 MODULE_PARM_DESC(w1_id, "1-wire id for the slave detection");
723 MODULE_AUTHOR("Texas Instruments");
724 MODULE_DESCRIPTION("HDQ driver Library");
725 MODULE_LICENSE("GPL");