2 * 1-wire busmaster driver for DS1WM and ASICs with embedded DS1WMs
3 * such as HP iPAQs (including h5xxx, h2200, and devices with ASIC3
6 * Copyright (c) 2004-2005, Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>
7 * Copyright (c) 2004-2007, Matt Reimer <mreimer@vpop.net>
9 * Use consistent with the GNU GPL is permitted,
10 * provided that this copyright notice is
11 * preserved in its entirety in all copies and derived works.
14 #include <linux/module.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
18 #include <linux/platform_device.h>
19 #include <linux/err.h>
20 #include <linux/delay.h>
21 #include <linux/mfd/core.h>
22 #include <linux/mfd/ds1wm.h>
23 #include <linux/slab.h>
28 #include "../w1_int.h"
31 #define DS1WM_CMD 0x00 /* R/W 4 bits command */
32 #define DS1WM_DATA 0x01 /* R/W 8 bits, transmit/receive buffer */
33 #define DS1WM_INT 0x02 /* R/W interrupt status */
34 #define DS1WM_INT_EN 0x03 /* R/W interrupt enable */
35 #define DS1WM_CLKDIV 0x04 /* R/W 5 bits of divisor and pre-scale */
36 #define DS1WM_CNTRL 0x05 /* R/W master control register (not used yet) */
38 #define DS1WM_CMD_1W_RESET (1 << 0) /* force reset on 1-wire bus */
39 #define DS1WM_CMD_SRA (1 << 1) /* enable Search ROM accelerator mode */
40 #define DS1WM_CMD_DQ_OUTPUT (1 << 2) /* write only - forces bus low */
41 #define DS1WM_CMD_DQ_INPUT (1 << 3) /* read only - reflects state of bus */
42 #define DS1WM_CMD_RST (1 << 5) /* software reset */
43 #define DS1WM_CMD_OD (1 << 7) /* overdrive */
45 #define DS1WM_INT_PD (1 << 0) /* presence detect */
46 #define DS1WM_INT_PDR (1 << 1) /* presence detect result */
47 #define DS1WM_INT_TBE (1 << 2) /* tx buffer empty */
48 #define DS1WM_INT_TSRE (1 << 3) /* tx shift register empty */
49 #define DS1WM_INT_RBF (1 << 4) /* rx buffer full */
50 #define DS1WM_INT_RSRF (1 << 5) /* rx shift register full */
52 #define DS1WM_INTEN_EPD (1 << 0) /* enable presence detect int */
53 #define DS1WM_INTEN_IAS (1 << 1) /* INTR active state */
54 #define DS1WM_INTEN_ETBE (1 << 2) /* enable tx buffer empty int */
55 #define DS1WM_INTEN_ETMT (1 << 3) /* enable tx shift register empty int */
56 #define DS1WM_INTEN_ERBF (1 << 4) /* enable rx buffer full int */
57 #define DS1WM_INTEN_ERSRF (1 << 5) /* enable rx shift register full int */
58 #define DS1WM_INTEN_DQO (1 << 6) /* enable direct bus driving ops */
60 #define DS1WM_INTEN_NOT_IAS (~DS1WM_INTEN_IAS) /* all but INTR active state */
62 #define DS1WM_TIMEOUT (HZ * 5)
66 unsigned long divisor;
92 /* you can continue this table, consult the OPERATION - CLOCK DIVISOR
93 section of the ds1wm spec sheet. */
98 int bus_shift; /* # of shifts to calc register offsets */
99 struct platform_device *pdev;
100 const struct mfd_cell *cell;
103 void *reset_complete;
105 void *write_complete;
107 /* last byte received */
109 /* byte to write that makes all intr disabled, */
110 /* considering active_state (IAS) (optimization) */
112 unsigned int reset_recover_delay; /* see ds1wm.h */
115 static inline void ds1wm_write_register(struct ds1wm_data *ds1wm_data, u32 reg,
118 __raw_writeb(val, ds1wm_data->map + (reg << ds1wm_data->bus_shift));
121 static inline u8 ds1wm_read_register(struct ds1wm_data *ds1wm_data, u32 reg)
123 return __raw_readb(ds1wm_data->map + (reg << ds1wm_data->bus_shift));
127 static irqreturn_t ds1wm_isr(int isr, void *data)
129 struct ds1wm_data *ds1wm_data = data;
131 u8 inten = ds1wm_read_register(ds1wm_data, DS1WM_INT_EN);
132 /* if no bits are set in int enable register (except the IAS)
133 than go no further, reading the regs below has side effects */
134 if (!(inten & DS1WM_INTEN_NOT_IAS))
137 ds1wm_write_register(ds1wm_data,
138 DS1WM_INT_EN, ds1wm_data->int_en_reg_none);
140 /* this read action clears the INTR and certain flags in ds1wm */
141 intr = ds1wm_read_register(ds1wm_data, DS1WM_INT);
143 ds1wm_data->slave_present = (intr & DS1WM_INT_PDR) ? 0 : 1;
145 if ((intr & DS1WM_INT_TSRE) && ds1wm_data->write_complete) {
146 inten &= ~DS1WM_INTEN_ETMT;
147 complete(ds1wm_data->write_complete);
149 if (intr & DS1WM_INT_RBF) {
150 /* this read clears the RBF flag */
151 ds1wm_data->read_byte = ds1wm_read_register(ds1wm_data,
153 inten &= ~DS1WM_INTEN_ERBF;
154 if (ds1wm_data->read_complete)
155 complete(ds1wm_data->read_complete);
157 if ((intr & DS1WM_INT_PD) && ds1wm_data->reset_complete) {
158 inten &= ~DS1WM_INTEN_EPD;
159 complete(ds1wm_data->reset_complete);
162 ds1wm_write_register(ds1wm_data, DS1WM_INT_EN, inten);
166 static int ds1wm_reset(struct ds1wm_data *ds1wm_data)
168 unsigned long timeleft;
169 DECLARE_COMPLETION_ONSTACK(reset_done);
171 ds1wm_data->reset_complete = &reset_done;
173 /* enable Presence detect only */
174 ds1wm_write_register(ds1wm_data, DS1WM_INT_EN, DS1WM_INTEN_EPD |
175 ds1wm_data->int_en_reg_none);
177 ds1wm_write_register(ds1wm_data, DS1WM_CMD, DS1WM_CMD_1W_RESET);
179 timeleft = wait_for_completion_timeout(&reset_done, DS1WM_TIMEOUT);
180 ds1wm_data->reset_complete = NULL;
182 dev_err(&ds1wm_data->pdev->dev, "reset failed, timed out\n");
186 if (!ds1wm_data->slave_present) {
187 dev_dbg(&ds1wm_data->pdev->dev, "reset: no devices found\n");
191 if (ds1wm_data->reset_recover_delay)
192 msleep(ds1wm_data->reset_recover_delay);
197 static int ds1wm_write(struct ds1wm_data *ds1wm_data, u8 data)
199 unsigned long timeleft;
200 DECLARE_COMPLETION_ONSTACK(write_done);
201 ds1wm_data->write_complete = &write_done;
203 ds1wm_write_register(ds1wm_data, DS1WM_INT_EN,
204 ds1wm_data->int_en_reg_none | DS1WM_INTEN_ETMT);
206 ds1wm_write_register(ds1wm_data, DS1WM_DATA, data);
208 timeleft = wait_for_completion_timeout(&write_done, DS1WM_TIMEOUT);
210 ds1wm_data->write_complete = NULL;
212 dev_err(&ds1wm_data->pdev->dev, "write failed, timed out\n");
219 static u8 ds1wm_read(struct ds1wm_data *ds1wm_data, unsigned char write_data)
221 unsigned long timeleft;
222 u8 intEnable = DS1WM_INTEN_ERBF | ds1wm_data->int_en_reg_none;
223 DECLARE_COMPLETION_ONSTACK(read_done);
225 ds1wm_read_register(ds1wm_data, DS1WM_DATA);
227 ds1wm_data->read_complete = &read_done;
228 ds1wm_write_register(ds1wm_data, DS1WM_INT_EN, intEnable);
230 ds1wm_write_register(ds1wm_data, DS1WM_DATA, write_data);
231 timeleft = wait_for_completion_timeout(&read_done, DS1WM_TIMEOUT);
233 ds1wm_data->read_complete = NULL;
235 dev_err(&ds1wm_data->pdev->dev, "read failed, timed out\n");
236 ds1wm_data->read_error = -ETIMEDOUT;
239 ds1wm_data->read_error = 0;
240 return ds1wm_data->read_byte;
243 static int ds1wm_find_divisor(int gclk)
247 for (i = ARRAY_SIZE(freq)-1; i >= 0; --i)
248 if (gclk >= freq[i].freq)
249 return freq[i].divisor;
254 static void ds1wm_up(struct ds1wm_data *ds1wm_data)
257 struct ds1wm_driver_data *plat = ds1wm_data->pdev->dev.platform_data;
259 if (ds1wm_data->cell->enable)
260 ds1wm_data->cell->enable(ds1wm_data->pdev);
262 divisor = ds1wm_find_divisor(plat->clock_rate);
263 dev_dbg(&ds1wm_data->pdev->dev,
264 "found divisor 0x%x for clock %d\n", divisor, plat->clock_rate);
266 dev_err(&ds1wm_data->pdev->dev,
267 "no suitable divisor for %dHz clock\n",
271 ds1wm_write_register(ds1wm_data, DS1WM_CLKDIV, divisor);
273 /* Let the w1 clock stabilize. */
276 ds1wm_reset(ds1wm_data);
279 static void ds1wm_down(struct ds1wm_data *ds1wm_data)
281 ds1wm_reset(ds1wm_data);
283 /* Disable interrupts. */
284 ds1wm_write_register(ds1wm_data, DS1WM_INT_EN,
285 ds1wm_data->int_en_reg_none);
287 if (ds1wm_data->cell->disable)
288 ds1wm_data->cell->disable(ds1wm_data->pdev);
291 /* --------------------------------------------------------------------- */
294 static u8 ds1wm_read_byte(void *data)
296 struct ds1wm_data *ds1wm_data = data;
298 return ds1wm_read(ds1wm_data, 0xff);
301 static void ds1wm_write_byte(void *data, u8 byte)
303 struct ds1wm_data *ds1wm_data = data;
305 ds1wm_write(ds1wm_data, byte);
308 static u8 ds1wm_reset_bus(void *data)
310 struct ds1wm_data *ds1wm_data = data;
312 ds1wm_reset(ds1wm_data);
317 static void ds1wm_search(void *data, struct w1_master *master_dev,
318 u8 search_type, w1_slave_found_callback slave_found)
320 struct ds1wm_data *ds1wm_data = data;
322 int ms_discrep_bit = -1;
323 u64 r = 0; /* holds the progress of the search */
325 unsigned slaves_found = 0;
326 unsigned int pass = 0;
328 dev_dbg(&ds1wm_data->pdev->dev, "search begin\n");
332 dev_dbg(&ds1wm_data->pdev->dev,
333 "too many attempts (100), search aborted\n");
337 mutex_lock(&master_dev->bus_mutex);
338 if (ds1wm_reset(ds1wm_data)) {
339 mutex_unlock(&master_dev->bus_mutex);
340 dev_dbg(&ds1wm_data->pdev->dev,
341 "pass: %d reset error (or no slaves)\n", pass);
345 dev_dbg(&ds1wm_data->pdev->dev,
346 "pass: %d r : %0#18llx writing SEARCH_ROM\n", pass, r);
347 ds1wm_write(ds1wm_data, search_type);
348 dev_dbg(&ds1wm_data->pdev->dev,
349 "pass: %d entering ASM\n", pass);
350 ds1wm_write_register(ds1wm_data, DS1WM_CMD, DS1WM_CMD_SRA);
351 dev_dbg(&ds1wm_data->pdev->dev,
352 "pass: %d begining nibble loop\n", pass);
356 /* we work one nibble at a time */
357 /* each nibble is interleaved to form a byte */
358 for (i = 0; i < 16; i++) {
360 unsigned char resp, _r, _r_prime, _d;
362 _r = (r >> (4*i)) & 0xf;
363 _r = ((_r & 0x1) << 1) |
368 /* writes _r, then reads back: */
369 resp = ds1wm_read(ds1wm_data, _r);
371 if (ds1wm_data->read_error) {
372 dev_err(&ds1wm_data->pdev->dev,
373 "pass: %d nibble: %d read error\n", pass, i);
377 _r_prime = ((resp & 0x02) >> 1) |
378 ((resp & 0x08) >> 2) |
379 ((resp & 0x20) >> 3) |
380 ((resp & 0x80) >> 4);
382 _d = ((resp & 0x01) >> 0) |
383 ((resp & 0x04) >> 1) |
384 ((resp & 0x10) >> 2) |
385 ((resp & 0x40) >> 3);
387 r_prime |= (unsigned long long) _r_prime << (i * 4);
388 d |= (unsigned long long) _d << (i * 4);
391 if (ds1wm_data->read_error) {
392 mutex_unlock(&master_dev->bus_mutex);
393 dev_err(&ds1wm_data->pdev->dev,
394 "pass: %d read error, retrying\n", pass);
397 dev_dbg(&ds1wm_data->pdev->dev,
398 "pass: %d r\': %0#18llx d:%0#18llx\n",
400 dev_dbg(&ds1wm_data->pdev->dev,
401 "pass: %d nibble loop complete, exiting ASM\n", pass);
402 ds1wm_write_register(ds1wm_data, DS1WM_CMD, ~DS1WM_CMD_SRA);
403 dev_dbg(&ds1wm_data->pdev->dev,
404 "pass: %d resetting bus\n", pass);
405 ds1wm_reset(ds1wm_data);
406 mutex_unlock(&master_dev->bus_mutex);
407 if ((r_prime & ((u64)1 << 63)) && (d & ((u64)1 << 63))) {
408 dev_err(&ds1wm_data->pdev->dev,
409 "pass: %d bus error, retrying\n", pass);
410 continue; /* start over */
414 dev_dbg(&ds1wm_data->pdev->dev,
415 "pass: %d found %0#18llx\n", pass, r_prime);
416 slave_found(master_dev, r_prime);
418 dev_dbg(&ds1wm_data->pdev->dev,
419 "pass: %d complete, preparing next pass\n", pass);
421 /* any discrepency found which we already choose the
422 '1' branch is now is now irrelevant we reveal the
423 next branch with this: */
425 /* find last bit set, i.e. the most signif. bit set */
426 ms_discrep_bit = fls64(d) - 1;
427 dev_dbg(&ds1wm_data->pdev->dev,
428 "pass: %d new d:%0#18llx MS discrep bit:%d\n",
429 pass, d, ms_discrep_bit);
431 /* prev_ms_discrep_bit = ms_discrep_bit;
432 prepare for next ROM search: */
433 if (ms_discrep_bit == -1)
436 r = (r & ~(~0ull << (ms_discrep_bit))) | 1 << ms_discrep_bit;
437 } /* end while true */
438 dev_dbg(&ds1wm_data->pdev->dev,
439 "pass: %d total: %d search done ms d bit pos: %d\n", pass,
440 slaves_found, ms_discrep_bit);
443 /* --------------------------------------------------------------------- */
445 static struct w1_bus_master ds1wm_master = {
446 .read_byte = ds1wm_read_byte,
447 .write_byte = ds1wm_write_byte,
448 .reset_bus = ds1wm_reset_bus,
449 .search = ds1wm_search,
452 static int ds1wm_probe(struct platform_device *pdev)
454 struct ds1wm_data *ds1wm_data;
455 struct ds1wm_driver_data *plat;
456 struct resource *res;
462 ds1wm_data = kzalloc(sizeof(*ds1wm_data), GFP_KERNEL);
466 platform_set_drvdata(pdev, ds1wm_data);
468 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
473 ds1wm_data->map = ioremap(res->start, resource_size(res));
474 if (!ds1wm_data->map) {
479 /* calculate bus shift from mem resource */
480 ds1wm_data->bus_shift = resource_size(res) >> 3;
482 ds1wm_data->pdev = pdev;
483 ds1wm_data->cell = mfd_get_cell(pdev);
484 if (!ds1wm_data->cell) {
488 plat = pdev->dev.platform_data;
494 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
499 ds1wm_data->irq = res->start;
500 ds1wm_data->int_en_reg_none = (plat->active_high ? DS1WM_INTEN_IAS : 0);
501 ds1wm_data->reset_recover_delay = plat->reset_recover_delay;
503 if (res->flags & IORESOURCE_IRQ_HIGHEDGE)
504 irq_set_irq_type(ds1wm_data->irq, IRQ_TYPE_EDGE_RISING);
505 if (res->flags & IORESOURCE_IRQ_LOWEDGE)
506 irq_set_irq_type(ds1wm_data->irq, IRQ_TYPE_EDGE_FALLING);
508 ret = request_irq(ds1wm_data->irq, ds1wm_isr,
509 IRQF_DISABLED | IRQF_SHARED, "ds1wm", ds1wm_data);
513 ds1wm_up(ds1wm_data);
515 ds1wm_master.data = (void *)ds1wm_data;
517 ret = w1_add_master_device(&ds1wm_master);
524 ds1wm_down(ds1wm_data);
525 free_irq(ds1wm_data->irq, ds1wm_data);
527 iounmap(ds1wm_data->map);
535 static int ds1wm_suspend(struct platform_device *pdev, pm_message_t state)
537 struct ds1wm_data *ds1wm_data = platform_get_drvdata(pdev);
539 ds1wm_down(ds1wm_data);
544 static int ds1wm_resume(struct platform_device *pdev)
546 struct ds1wm_data *ds1wm_data = platform_get_drvdata(pdev);
548 ds1wm_up(ds1wm_data);
553 #define ds1wm_suspend NULL
554 #define ds1wm_resume NULL
557 static int ds1wm_remove(struct platform_device *pdev)
559 struct ds1wm_data *ds1wm_data = platform_get_drvdata(pdev);
561 w1_remove_master_device(&ds1wm_master);
562 ds1wm_down(ds1wm_data);
563 free_irq(ds1wm_data->irq, ds1wm_data);
564 iounmap(ds1wm_data->map);
570 static struct platform_driver ds1wm_driver = {
574 .probe = ds1wm_probe,
575 .remove = ds1wm_remove,
576 .suspend = ds1wm_suspend,
577 .resume = ds1wm_resume
580 static int __init ds1wm_init(void)
582 printk("DS1WM w1 busmaster driver - (c) 2004 Szabolcs Gyurko\n");
583 return platform_driver_register(&ds1wm_driver);
586 static void __exit ds1wm_exit(void)
588 platform_driver_unregister(&ds1wm_driver);
591 module_init(ds1wm_init);
592 module_exit(ds1wm_exit);
594 MODULE_LICENSE("GPL");
595 MODULE_AUTHOR("Szabolcs Gyurko <szabolcs.gyurko@tlt.hu>, "
596 "Matt Reimer <mreimer@vpop.net>,"
597 "Jean-Francois Dagenais <dagenaisj@sonatest.com>");
598 MODULE_DESCRIPTION("DS1WM w1 busmaster driver");