]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/i2c/busses/i2c-octeon.c
i2c: octeon: Cleanup resource allocation code
[mirror_ubuntu-artful-kernel.git] / drivers / i2c / busses / i2c-octeon.c
1 /*
2 * (C) Copyright 2009-2010
3 * Nokia Siemens Networks, michael.lawnick.ext@nsn.com
4 *
5 * Portions Copyright (C) 2010 - 2016 Cavium, Inc.
6 *
7 * This is a driver for the i2c adapter in Cavium Networks' OCTEON processors.
8 *
9 * This file is licensed under the terms of the GNU General Public
10 * License version 2. This program is licensed "as is" without any
11 * warranty of any kind, whether express or implied.
12 */
13
14 #include <linux/platform_device.h>
15 #include <linux/interrupt.h>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/delay.h>
19 #include <linux/sched.h>
20 #include <linux/slab.h>
21 #include <linux/i2c.h>
22 #include <linux/io.h>
23 #include <linux/of.h>
24
25 #include <asm/octeon/octeon.h>
26
27 #define DRV_NAME "i2c-octeon"
28
29 /* Register offsets */
30 #define SW_TWSI 0x00
31 #define TWSI_INT 0x10
32
33 /* Controller command patterns */
34 #define SW_TWSI_V BIT_ULL(63) /* Valid bit */
35 #define SW_TWSI_R BIT_ULL(56) /* Result or read bit */
36
37 /* Controller opcode word (bits 60:57) */
38 #define SW_TWSI_OP_SHIFT 57
39 #define SW_TWSI_OP_TWSI_CLK (4ULL << SW_TWSI_OP_SHIFT)
40 #define SW_TWSI_OP_EOP (6ULL << SW_TWSI_OP_SHIFT) /* Extended opcode */
41
42 /* Controller extended opcode word (bits 34:32) */
43 #define SW_TWSI_EOP_SHIFT 32
44 #define SW_TWSI_EOP_TWSI_DATA (SW_TWSI_OP_EOP | 1ULL << SW_TWSI_EOP_SHIFT)
45 #define SW_TWSI_EOP_TWSI_CTL (SW_TWSI_OP_EOP | 2ULL << SW_TWSI_EOP_SHIFT)
46 #define SW_TWSI_EOP_TWSI_CLKCTL (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
47 #define SW_TWSI_EOP_TWSI_STAT (SW_TWSI_OP_EOP | 3ULL << SW_TWSI_EOP_SHIFT)
48 #define SW_TWSI_EOP_TWSI_RST (SW_TWSI_OP_EOP | 7ULL << SW_TWSI_EOP_SHIFT)
49
50 /* Controller command and status bits */
51 #define TWSI_CTL_CE 0x80
52 #define TWSI_CTL_ENAB 0x40 /* Bus enable */
53 #define TWSI_CTL_STA 0x20 /* Master-mode start, HW clears when done */
54 #define TWSI_CTL_STP 0x10 /* Master-mode stop, HW clears when done */
55 #define TWSI_CTL_IFLG 0x08 /* HW event, SW writes 0 to ACK */
56 #define TWSI_CTL_AAK 0x04 /* Assert ACK */
57
58 /* Some status values */
59 #define STAT_START 0x08
60 #define STAT_RSTART 0x10
61 #define STAT_TXADDR_ACK 0x18
62 #define STAT_TXDATA_ACK 0x28
63 #define STAT_RXADDR_ACK 0x40
64 #define STAT_RXDATA_ACK 0x50
65 #define STAT_IDLE 0xF8
66
67 /* TWSI_INT values */
68 #define TWSI_INT_CORE_EN BIT_ULL(6)
69 #define TWSI_INT_SDA_OVR BIT_ULL(8)
70 #define TWSI_INT_SCL_OVR BIT_ULL(9)
71
72 struct octeon_i2c {
73 wait_queue_head_t queue;
74 struct i2c_adapter adap;
75 int irq;
76 u32 twsi_freq;
77 int sys_freq;
78 void __iomem *twsi_base;
79 struct device *dev;
80 };
81
82 /**
83 * octeon_i2c_write_sw - write an I2C core register
84 * @i2c: The struct octeon_i2c
85 * @eop_reg: Register selector
86 * @data: Value to be written
87 *
88 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
89 */
90 static void octeon_i2c_write_sw(struct octeon_i2c *i2c, u64 eop_reg, u8 data)
91 {
92 u64 tmp;
93
94 __raw_writeq(SW_TWSI_V | eop_reg | data, i2c->twsi_base + SW_TWSI);
95 do {
96 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
97 } while ((tmp & SW_TWSI_V) != 0);
98 }
99
100 /**
101 * octeon_i2c_read_sw - read lower bits of an I2C core register
102 * @i2c: The struct octeon_i2c
103 * @eop_reg: Register selector
104 *
105 * Returns the data.
106 *
107 * The I2C core registers are accessed indirectly via the SW_TWSI CSR.
108 */
109 static u8 octeon_i2c_read_sw(struct octeon_i2c *i2c, u64 eop_reg)
110 {
111 u64 tmp;
112
113 __raw_writeq(SW_TWSI_V | eop_reg | SW_TWSI_R, i2c->twsi_base + SW_TWSI);
114 do {
115 tmp = __raw_readq(i2c->twsi_base + SW_TWSI);
116 } while ((tmp & SW_TWSI_V) != 0);
117
118 return tmp & 0xFF;
119 }
120
121 /**
122 * octeon_i2c_write_int - write the TWSI_INT register
123 * @i2c: The struct octeon_i2c
124 * @data: Value to be written
125 */
126 static void octeon_i2c_write_int(struct octeon_i2c *i2c, u64 data)
127 {
128 __raw_writeq(data, i2c->twsi_base + TWSI_INT);
129 __raw_readq(i2c->twsi_base + TWSI_INT);
130 }
131
132 /**
133 * octeon_i2c_int_enable - enable the CORE interrupt
134 * @i2c: The struct octeon_i2c
135 *
136 * The interrupt will be asserted when there is non-STAT_IDLE state in
137 * the SW_TWSI_EOP_TWSI_STAT register.
138 */
139 static void octeon_i2c_int_enable(struct octeon_i2c *i2c)
140 {
141 octeon_i2c_write_int(i2c, TWSI_INT_CORE_EN);
142 }
143
144 /* disable the CORE interrupt */
145 static void octeon_i2c_int_disable(struct octeon_i2c *i2c)
146 {
147 /* clear TS/ST/IFLG events */
148 octeon_i2c_write_int(i2c, 0);
149 }
150
151 /**
152 * octeon_i2c_unblock - unblock the bus
153 * @i2c: The struct octeon_i2c
154 *
155 * If there was a reset while a device was driving 0 to bus, bus is blocked.
156 * We toggle it free manually by some clock cycles and send a stop.
157 */
158 static void octeon_i2c_unblock(struct octeon_i2c *i2c)
159 {
160 int i;
161
162 dev_dbg(i2c->dev, "%s\n", __func__);
163
164 for (i = 0; i < 9; i++) {
165 octeon_i2c_write_int(i2c, 0);
166 udelay(5);
167 octeon_i2c_write_int(i2c, TWSI_INT_SCL_OVR);
168 udelay(5);
169 }
170 /* hand-crank a STOP */
171 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR | TWSI_INT_SCL_OVR);
172 udelay(5);
173 octeon_i2c_write_int(i2c, TWSI_INT_SDA_OVR);
174 udelay(5);
175 octeon_i2c_write_int(i2c, 0);
176 }
177
178 /* interrupt service routine */
179 static irqreturn_t octeon_i2c_isr(int irq, void *dev_id)
180 {
181 struct octeon_i2c *i2c = dev_id;
182
183 octeon_i2c_int_disable(i2c);
184 wake_up(&i2c->queue);
185
186 return IRQ_HANDLED;
187 }
188
189
190 static int octeon_i2c_test_iflg(struct octeon_i2c *i2c)
191 {
192 return (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_CTL) & TWSI_CTL_IFLG) != 0;
193 }
194
195 /**
196 * octeon_i2c_wait - wait for the IFLG to be set
197 * @i2c: The struct octeon_i2c
198 *
199 * Returns 0 on success, otherwise a negative errno.
200 */
201 static int octeon_i2c_wait(struct octeon_i2c *i2c)
202 {
203 long time_left;
204
205 octeon_i2c_int_enable(i2c);
206 time_left = wait_event_timeout(i2c->queue, octeon_i2c_test_iflg(i2c),
207 i2c->adap.timeout);
208 octeon_i2c_int_disable(i2c);
209 if (!time_left) {
210 dev_dbg(i2c->dev, "%s: timeout\n", __func__);
211 return -ETIMEDOUT;
212 }
213
214 return 0;
215 }
216
217 /**
218 * octeon_i2c_start - send START to the bus
219 * @i2c: The struct octeon_i2c
220 *
221 * Returns 0 on success, otherwise a negative errno.
222 */
223 static int octeon_i2c_start(struct octeon_i2c *i2c)
224 {
225 int result;
226 u8 data;
227
228 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
229 TWSI_CTL_ENAB | TWSI_CTL_STA);
230
231 result = octeon_i2c_wait(i2c);
232 if (result) {
233 if (octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT) == STAT_IDLE) {
234 /*
235 * Controller refused to send start flag May
236 * be a client is holding SDA low - let's try
237 * to free it.
238 */
239 octeon_i2c_unblock(i2c);
240 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
241 TWSI_CTL_ENAB | TWSI_CTL_STA);
242 result = octeon_i2c_wait(i2c);
243 }
244 if (result)
245 return result;
246 }
247
248 data = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
249 if ((data != STAT_START) && (data != STAT_RSTART)) {
250 dev_err(i2c->dev, "%s: bad status (0x%x)\n", __func__, data);
251 return -EIO;
252 }
253
254 return 0;
255 }
256
257 /* send STOP to the bus */
258 static void octeon_i2c_stop(struct octeon_i2c *i2c)
259 {
260 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
261 TWSI_CTL_ENAB | TWSI_CTL_STP);
262 }
263
264 /**
265 * octeon_i2c_write - send data to the bus via low-level controller
266 * @i2c: The struct octeon_i2c
267 * @target: Target address
268 * @data: Pointer to the data to be sent
269 * @length: Length of the data
270 *
271 * The address is sent over the bus, then the data.
272 *
273 * Returns 0 on success, otherwise a negative errno.
274 */
275 static int octeon_i2c_write(struct octeon_i2c *i2c, int target,
276 const u8 *data, int length)
277 {
278 int i, result;
279 u8 tmp;
280
281 result = octeon_i2c_start(i2c);
282 if (result)
283 return result;
284
285 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, target << 1);
286 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
287
288 result = octeon_i2c_wait(i2c);
289 if (result)
290 return result;
291
292 for (i = 0; i < length; i++) {
293 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
294
295 if ((tmp != STAT_TXADDR_ACK) && (tmp != STAT_TXDATA_ACK)) {
296 dev_err(i2c->dev,
297 "%s: bad status before write (0x%x)\n",
298 __func__, tmp);
299 return -EIO;
300 }
301
302 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, data[i]);
303 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
304
305 result = octeon_i2c_wait(i2c);
306 if (result)
307 return result;
308 }
309
310 return 0;
311 }
312
313 /**
314 * octeon_i2c_read - receive data from the bus via low-level controller
315 * @i2c: The struct octeon_i2c
316 * @target: Target address
317 * @data: Pointer to the location to store the data
318 * @length: Length of the data
319 *
320 * The address is sent over the bus, then the data is read.
321 *
322 * Returns 0 on success, otherwise a negative errno.
323 */
324 static int octeon_i2c_read(struct octeon_i2c *i2c, int target,
325 u8 *data, int length)
326 {
327 int i, result;
328 u8 tmp;
329
330 if (length < 1)
331 return -EINVAL;
332
333 result = octeon_i2c_start(i2c);
334 if (result)
335 return result;
336
337 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_DATA, (target << 1) | 1);
338 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
339
340 result = octeon_i2c_wait(i2c);
341 if (result)
342 return result;
343
344 for (i = 0; i < length; i++) {
345 tmp = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
346
347 if ((tmp != STAT_RXDATA_ACK) && (tmp != STAT_RXADDR_ACK)) {
348 dev_err(i2c->dev,
349 "%s: bad status before read (0x%x)\n",
350 __func__, tmp);
351 return -EIO;
352 }
353
354 if (i + 1 < length)
355 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
356 TWSI_CTL_ENAB | TWSI_CTL_AAK);
357 else
358 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL,
359 TWSI_CTL_ENAB);
360
361 result = octeon_i2c_wait(i2c);
362 if (result)
363 return result;
364
365 data[i] = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_DATA);
366 }
367 return 0;
368 }
369
370 /**
371 * octeon_i2c_xfer - The driver's master_xfer function
372 * @adap: Pointer to the i2c_adapter structure
373 * @msgs: Pointer to the messages to be processed
374 * @num: Length of the MSGS array
375 *
376 * Returns the number of messages processed, or a negative errno on failure.
377 */
378 static int octeon_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
379 int num)
380 {
381 struct octeon_i2c *i2c = i2c_get_adapdata(adap);
382 int i, ret = 0;
383
384 for (i = 0; ret == 0 && i < num; i++) {
385 struct i2c_msg *pmsg = &msgs[i];
386
387 dev_dbg(i2c->dev,
388 "Doing %s %d byte(s) to/from 0x%02x - %d of %d messages\n",
389 pmsg->flags & I2C_M_RD ? "read" : "write",
390 pmsg->len, pmsg->addr, i + 1, num);
391 if (pmsg->flags & I2C_M_RD)
392 ret = octeon_i2c_read(i2c, pmsg->addr, pmsg->buf,
393 pmsg->len);
394 else
395 ret = octeon_i2c_write(i2c, pmsg->addr, pmsg->buf,
396 pmsg->len);
397 }
398 octeon_i2c_stop(i2c);
399
400 return (ret != 0) ? ret : num;
401 }
402
403 static u32 octeon_i2c_functionality(struct i2c_adapter *adap)
404 {
405 return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;
406 }
407
408 static const struct i2c_algorithm octeon_i2c_algo = {
409 .master_xfer = octeon_i2c_xfer,
410 .functionality = octeon_i2c_functionality,
411 };
412
413 static struct i2c_adapter octeon_i2c_ops = {
414 .owner = THIS_MODULE,
415 .name = "OCTEON adapter",
416 .algo = &octeon_i2c_algo,
417 .timeout = HZ / 50,
418 };
419
420 /* calculate and set clock divisors */
421 static void octeon_i2c_set_clock(struct octeon_i2c *i2c)
422 {
423 int tclk, thp_base, inc, thp_idx, mdiv_idx, ndiv_idx, foscl, diff;
424 int thp = 0x18, mdiv = 2, ndiv = 0, delta_hz = 1000000;
425
426 for (ndiv_idx = 0; ndiv_idx < 8 && delta_hz != 0; ndiv_idx++) {
427 /*
428 * An mdiv value of less than 2 seems to not work well
429 * with ds1337 RTCs, so we constrain it to larger values.
430 */
431 for (mdiv_idx = 15; mdiv_idx >= 2 && delta_hz != 0; mdiv_idx--) {
432 /*
433 * For given ndiv and mdiv values check the
434 * two closest thp values.
435 */
436 tclk = i2c->twsi_freq * (mdiv_idx + 1) * 10;
437 tclk *= (1 << ndiv_idx);
438 thp_base = (i2c->sys_freq / (tclk * 2)) - 1;
439
440 for (inc = 0; inc <= 1; inc++) {
441 thp_idx = thp_base + inc;
442 if (thp_idx < 5 || thp_idx > 0xff)
443 continue;
444
445 foscl = i2c->sys_freq / (2 * (thp_idx + 1));
446 foscl = foscl / (1 << ndiv_idx);
447 foscl = foscl / (mdiv_idx + 1) / 10;
448 diff = abs(foscl - i2c->twsi_freq);
449 if (diff < delta_hz) {
450 delta_hz = diff;
451 thp = thp_idx;
452 mdiv = mdiv_idx;
453 ndiv = ndiv_idx;
454 }
455 }
456 }
457 }
458 octeon_i2c_write_sw(i2c, SW_TWSI_OP_TWSI_CLK, thp);
459 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CLKCTL, (mdiv << 3) | ndiv);
460 }
461
462 static int octeon_i2c_init_lowlevel(struct octeon_i2c *i2c)
463 {
464 u8 status;
465 int tries;
466
467 /* disable high level controller, enable bus access */
468 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_CTL, TWSI_CTL_ENAB);
469
470 /* reset controller */
471 octeon_i2c_write_sw(i2c, SW_TWSI_EOP_TWSI_RST, 0);
472
473 for (tries = 10; tries; tries--) {
474 udelay(1);
475 status = octeon_i2c_read_sw(i2c, SW_TWSI_EOP_TWSI_STAT);
476 if (status == STAT_IDLE)
477 return 0;
478 }
479 dev_err(i2c->dev, "%s: TWSI_RST failed! (0x%x)\n", __func__, status);
480 return -EIO;
481 }
482
483 static int octeon_i2c_probe(struct platform_device *pdev)
484 {
485 struct device_node *node = pdev->dev.of_node;
486 struct resource *res_mem;
487 struct octeon_i2c *i2c;
488 int irq, result = 0;
489
490 /* All adaptors have an irq. */
491 irq = platform_get_irq(pdev, 0);
492 if (irq < 0)
493 return irq;
494
495 i2c = devm_kzalloc(&pdev->dev, sizeof(*i2c), GFP_KERNEL);
496 if (!i2c) {
497 result = -ENOMEM;
498 goto out;
499 }
500 i2c->dev = &pdev->dev;
501
502 res_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
503 i2c->twsi_base = devm_ioremap_resource(&pdev->dev, res_mem);
504 if (IS_ERR(i2c->twsi_base)) {
505 result = PTR_ERR(i2c->twsi_base);
506 goto out;
507 }
508
509 /*
510 * "clock-rate" is a legacy binding, the official binding is
511 * "clock-frequency". Try the official one first and then
512 * fall back if it doesn't exist.
513 */
514 if (of_property_read_u32(node, "clock-frequency", &i2c->twsi_freq) &&
515 of_property_read_u32(node, "clock-rate", &i2c->twsi_freq)) {
516 dev_err(i2c->dev,
517 "no I2C 'clock-rate' or 'clock-frequency' property\n");
518 result = -ENXIO;
519 goto out;
520 }
521
522 i2c->sys_freq = octeon_get_io_clock_rate();
523
524 init_waitqueue_head(&i2c->queue);
525
526 i2c->irq = irq;
527
528 result = devm_request_irq(&pdev->dev, i2c->irq,
529 octeon_i2c_isr, 0, DRV_NAME, i2c);
530 if (result < 0) {
531 dev_err(i2c->dev, "failed to attach interrupt\n");
532 goto out;
533 }
534
535 result = octeon_i2c_init_lowlevel(i2c);
536 if (result) {
537 dev_err(i2c->dev, "init low level failed\n");
538 goto out;
539 }
540
541 octeon_i2c_set_clock(i2c);
542
543 i2c->adap = octeon_i2c_ops;
544 i2c->adap.dev.parent = &pdev->dev;
545 i2c->adap.dev.of_node = node;
546 i2c_set_adapdata(&i2c->adap, i2c);
547 platform_set_drvdata(pdev, i2c);
548
549 result = i2c_add_adapter(&i2c->adap);
550 if (result < 0) {
551 dev_err(i2c->dev, "failed to add adapter\n");
552 goto out;
553 }
554 dev_info(i2c->dev, "probed\n");
555 return 0;
556
557 out:
558 return result;
559 };
560
561 static int octeon_i2c_remove(struct platform_device *pdev)
562 {
563 struct octeon_i2c *i2c = platform_get_drvdata(pdev);
564
565 i2c_del_adapter(&i2c->adap);
566 return 0;
567 };
568
569 static const struct of_device_id octeon_i2c_match[] = {
570 { .compatible = "cavium,octeon-3860-twsi", },
571 {},
572 };
573 MODULE_DEVICE_TABLE(of, octeon_i2c_match);
574
575 static struct platform_driver octeon_i2c_driver = {
576 .probe = octeon_i2c_probe,
577 .remove = octeon_i2c_remove,
578 .driver = {
579 .name = DRV_NAME,
580 .of_match_table = octeon_i2c_match,
581 },
582 };
583
584 module_platform_driver(octeon_i2c_driver);
585
586 MODULE_AUTHOR("Michael Lawnick <michael.lawnick.ext@nsn.com>");
587 MODULE_DESCRIPTION("I2C-Bus adapter for Cavium OCTEON processors");
588 MODULE_LICENSE("GPL");