]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm64/rockchip/rk_i2c.c
Upgrade to Bzip2 version 1.0.7.
[FreeBSD/FreeBSD.git] / sys / arm64 / rockchip / rk_i2c.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/bus.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
36 #include <sys/rman.h>
37 #include <machine/bus.h>
38
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/ofw_bus_subr.h>
41
42 #include <dev/iicbus/iiconf.h>
43 #include <dev/iicbus/iicbus.h>
44
45 #include <dev/extres/clk/clk.h>
46
47 #include "iicbus_if.h"
48
49 #include "opt_soc.h"
50
51
52 #define RK_I2C_CON                      0x00
53 #define  RK_I2C_CON_EN                  (1 << 0)
54 #define  RK_I2C_CON_MODE_SHIFT          1
55 #define  RK_I2C_CON_MODE_TX             0
56 #define  RK_I2C_CON_MODE_RRX            1
57 #define  RK_I2C_CON_MODE_RX             2
58 #define  RK_I2C_CON_MODE_RTX            3
59 #define  RK_I2C_CON_MODE_MASK           0x6
60 #define  RK_I2C_CON_START               (1 << 3)
61 #define  RK_I2C_CON_STOP                (1 << 4)
62 #define  RK_I2C_CON_LASTACK             (1 << 5)
63 #define  RK_I2C_CON_NAKSTOP             (1 << 6)
64
65 #define RK_I2C_CLKDIV           0x04
66 #define  RK_I2C_CLKDIVL_MASK    0xFFFF
67 #define  RK_I2C_CLKDIVL_SHIFT   0
68 #define  RK_I2C_CLKDIVH_MASK    0xFFFF0000
69 #define  RK_I2C_CLKDIVH_SHIFT   16
70 #define  RK_I2C_CLKDIV_MUL      8
71
72 #define RK_I2C_MRXADDR                  0x08
73 #define  RK_I2C_MRXADDR_SADDR_MASK      0xFFFFFF
74 #define  RK_I2C_MRXADDR_VALID(x)        (1 << (24 + x))
75
76 #define RK_I2C_MRXRADDR                 0x0C
77 #define  RK_I2C_MRXRADDR_SRADDR_MASK    0xFFFFFF
78 #define  RK_I2C_MRXRADDR_VALID(x)       (1 << (24 + x))
79
80 #define RK_I2C_MTXCNT           0x10
81 #define  RK_I2C_MTXCNT_MASK     0x3F
82
83 #define RK_I2C_MRXCNT           0x14
84 #define  RK_I2C_MRXCNT_MASK     0x3F
85
86 #define RK_I2C_IEN              0x18
87 #define  RK_I2C_IEN_BTFIEN      (1 << 0)
88 #define  RK_I2C_IEN_BRFIEN      (1 << 1)
89 #define  RK_I2C_IEN_MBTFIEN     (1 << 2)
90 #define  RK_I2C_IEN_MBRFIEN     (1 << 3)
91 #define  RK_I2C_IEN_STARTIEN    (1 << 4)
92 #define  RK_I2C_IEN_STOPIEN     (1 << 5)
93 #define  RK_I2C_IEN_NAKRCVIEN   (1 << 6)
94 #define  RK_I2C_IEN_ALL         (RK_I2C_IEN_BTFIEN | \
95         RK_I2C_IEN_BRFIEN | RK_I2C_IEN_MBTFIEN | RK_I2C_IEN_MBRFIEN | \
96         RK_I2C_IEN_STARTIEN | RK_I2C_IEN_STOPIEN | RK_I2C_IEN_NAKRCVIEN)
97
98 #define RK_I2C_IPD              0x1C
99 #define  RK_I2C_IPD_BTFIPD      (1 << 0)
100 #define  RK_I2C_IPD_BRFIPD      (1 << 1)
101 #define  RK_I2C_IPD_MBTFIPD     (1 << 2)
102 #define  RK_I2C_IPD_MBRFIPD     (1 << 3)
103 #define  RK_I2C_IPD_STARTIPD    (1 << 4)
104 #define  RK_I2C_IPD_STOPIPD     (1 << 5)
105 #define  RK_I2C_IPD_NAKRCVIPD   (1 << 6)
106 #define  RK_I2C_IPD_ALL         (RK_I2C_IPD_BTFIPD | \
107         RK_I2C_IPD_BRFIPD | RK_I2C_IPD_MBTFIPD | RK_I2C_IPD_MBRFIPD | \
108         RK_I2C_IPD_STARTIPD | RK_I2C_IPD_STOPIPD | RK_I2C_IPD_NAKRCVIPD)
109
110 #define RK_I2C_FNCT             0x20
111 #define  RK_I2C_FNCT_MASK       0x3F
112
113 #define RK_I2C_TXDATA_BASE      0x100
114
115 #define RK_I2C_RXDATA_BASE      0x200
116
117 enum rk_i2c_state {
118         STATE_IDLE = 0,
119         STATE_START,
120         STATE_READ,
121         STATE_WRITE,
122         STATE_STOP
123 };
124
125 struct rk_i2c_softc {
126         device_t        dev;
127         struct resource *res[2];
128         struct mtx      mtx;
129         clk_t           sclk;
130         clk_t           pclk;
131         int             busy;
132         void *          intrhand;
133         uint32_t        intr;
134         uint32_t        ipd;
135         struct iic_msg  *msg;
136         size_t          cnt;
137         int             transfer_done;
138         int             nak_recv;
139         uint8_t         mode;
140         uint8_t         state;
141
142         device_t        iicbus;
143 };
144
145 static struct ofw_compat_data compat_data[] = {
146 #ifdef SOC_ROCKCHIP_RK3328
147         {"rockchip,rk3328-i2c", 1},
148 #endif
149 #ifdef SOC_ROCKCHIP_RK3399
150         {"rockchip,rk3399-i2c", 1},
151 #endif
152         {NULL,             0}
153 };
154
155 static struct resource_spec rk_i2c_spec[] = {
156         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
157         { SYS_RES_IRQ,          0,      RF_ACTIVE | RF_SHAREABLE },
158         { -1, 0 }
159 };
160
161 static int rk_i2c_probe(device_t dev);
162 static int rk_i2c_attach(device_t dev);
163 static int rk_i2c_detach(device_t dev);
164
165 #define RK_I2C_LOCK(sc)                 mtx_lock(&(sc)->mtx)
166 #define RK_I2C_UNLOCK(sc)               mtx_unlock(&(sc)->mtx)
167 #define RK_I2C_ASSERT_LOCKED(sc)        mtx_assert(&(sc)->mtx, MA_OWNED)
168 #define RK_I2C_READ(sc, reg)            bus_read_4((sc)->res[0], (reg))
169 #define RK_I2C_WRITE(sc, reg, val)      bus_write_4((sc)->res[0], (reg), (val))
170
171 static uint32_t
172 rk_i2c_get_clkdiv(struct rk_i2c_softc *sc, uint64_t speed)
173 {
174         uint64_t sclk_freq;
175         uint32_t clkdiv;
176         int err;
177
178         err = clk_get_freq(sc->sclk, &sclk_freq);
179         if (err != 0)
180                 return (err);
181
182         clkdiv = (sclk_freq / speed / RK_I2C_CLKDIV_MUL / 2) - 1;
183         clkdiv &= RK_I2C_CLKDIVL_MASK;
184
185         clkdiv = clkdiv << RK_I2C_CLKDIVH_SHIFT | clkdiv;
186
187         return (clkdiv);
188 }
189
190 static int
191 rk_i2c_reset(device_t dev, u_char speed, u_char addr, u_char *oldaddr)
192 {
193         struct rk_i2c_softc *sc;
194         uint32_t clkdiv;
195         u_int busfreq;
196
197         sc = device_get_softc(dev);
198
199         busfreq = IICBUS_GET_FREQUENCY(sc->iicbus, speed);
200
201         clkdiv = rk_i2c_get_clkdiv(sc, busfreq);
202
203         RK_I2C_LOCK(sc);
204
205         /* Set the clock divider */
206         RK_I2C_WRITE(sc, RK_I2C_CLKDIV, clkdiv);
207
208         /* Disable the module */
209         RK_I2C_WRITE(sc, RK_I2C_CON, 0);
210
211         RK_I2C_UNLOCK(sc);
212
213         return (0);
214 }
215
216 static void
217 rk_i2c_fill_tx(struct rk_i2c_softc *sc)
218 {
219         uint32_t buf32;
220         uint8_t buf;
221         int i, j, len;
222
223         if (sc->msg == NULL || sc->msg->len == sc->cnt)
224                 return;
225
226         len = sc->msg->len - sc->cnt;
227         if (len > 8)
228                 len = 8;
229
230         for (i = 0; i < len; i++) {
231                 buf32 = 0;
232                 for (j = 0; j < 4 ; j++) {
233                         if (sc->cnt == sc->msg->len)
234                                 break;
235
236                         /* Fill the addr if needed */
237                         if (sc->cnt == 0) {
238                                 buf = sc->msg->slave;
239                         }
240                         else
241                                 buf = sc->msg->buf[sc->cnt - 1];
242
243                         buf32 |= buf << (j * 8);
244
245                         sc->cnt++;
246                 }
247
248                 RK_I2C_WRITE(sc, RK_I2C_TXDATA_BASE + 4 * i, buf32);
249
250                 if (sc->cnt == sc->msg->len)
251                         break;
252         }
253 }
254
255 static void
256 rk_i2c_drain_rx(struct rk_i2c_softc *sc)
257 {
258         uint32_t buf32 = 0;
259         uint8_t buf8;
260         int len;
261         int i;
262
263         if (sc->msg == NULL) {
264                 device_printf(sc->dev, "No current iic msg\n");
265                 return;
266         }
267
268         len = sc->msg->len - sc->cnt;
269         if (len > 32)
270                 len = 32;
271
272         for (i = 0; i < len; i++) {
273                 if (i % 4 == 0)
274                         buf32 = RK_I2C_READ(sc, RK_I2C_RXDATA_BASE + (i / 4) * 4);
275
276                 buf8 = (buf32 >> ((i % 4) * 8)) & 0xFF;
277
278                 sc->msg->buf[sc->cnt++] = buf8;
279         }
280 }
281
282 static void
283 rk_i2c_send_start(struct rk_i2c_softc *sc)
284 {
285         uint32_t reg;
286
287         RK_I2C_WRITE(sc, RK_I2C_IEN, RK_I2C_IEN_STARTIEN);
288
289         sc->state = STATE_START;
290
291         reg = RK_I2C_READ(sc, RK_I2C_CON);
292         reg |= RK_I2C_CON_START;
293         reg |= RK_I2C_CON_EN;
294         reg &= ~RK_I2C_CON_MODE_MASK;
295         reg |= sc->mode << RK_I2C_CON_MODE_SHIFT;
296         RK_I2C_WRITE(sc, RK_I2C_CON, reg);
297 }
298
299 static void
300 rk_i2c_send_stop(struct rk_i2c_softc *sc)
301 {
302         uint32_t reg;
303
304         RK_I2C_WRITE(sc, RK_I2C_IEN, RK_I2C_IEN_STOPIEN);
305
306         sc->state = STATE_STOP;
307
308         reg = RK_I2C_READ(sc, RK_I2C_CON);
309         reg |= RK_I2C_CON_STOP;
310         RK_I2C_WRITE(sc, RK_I2C_CON, reg);
311 }
312
313 static void
314 rk_i2c_intr(void *arg)
315 {
316         struct rk_i2c_softc *sc;
317         uint32_t reg;
318
319         sc = (struct rk_i2c_softc *)arg;
320
321         RK_I2C_LOCK(sc);
322
323         sc->ipd = RK_I2C_READ(sc, RK_I2C_IPD);
324         RK_I2C_WRITE(sc, RK_I2C_IPD, sc->ipd);
325
326         switch (sc->state) {
327         case STATE_START:
328                 /* Disable start bit */
329                 reg = RK_I2C_READ(sc, RK_I2C_CON);
330                 reg &= ~RK_I2C_CON_START;
331                 RK_I2C_WRITE(sc, RK_I2C_CON, reg);
332
333                 if (sc->mode == RK_I2C_CON_MODE_RRX ||
334                     sc->mode == RK_I2C_CON_MODE_RX) {
335                         sc->state = STATE_READ;
336                         RK_I2C_WRITE(sc, RK_I2C_IEN, RK_I2C_IEN_MBRFIEN |
337                             RK_I2C_IEN_NAKRCVIEN);
338
339                         reg = RK_I2C_READ(sc, RK_I2C_CON);
340                         reg |= RK_I2C_CON_LASTACK;
341                         RK_I2C_WRITE(sc, RK_I2C_CON, reg);
342
343                         RK_I2C_WRITE(sc, RK_I2C_MRXCNT, sc->msg->len);
344                 } else {
345                         sc->state = STATE_WRITE;
346                         RK_I2C_WRITE(sc, RK_I2C_IEN, RK_I2C_IEN_MBTFIEN |
347                             RK_I2C_IEN_NAKRCVIEN);
348
349                         sc->msg->len += 1;
350                         rk_i2c_fill_tx(sc);
351                         RK_I2C_WRITE(sc, RK_I2C_MTXCNT, sc->msg->len);
352                 }
353                 break;
354         case STATE_READ:
355                 rk_i2c_drain_rx(sc);
356
357                 if (sc->cnt == sc->msg->len)
358                         rk_i2c_send_stop(sc);
359
360                 break;
361         case STATE_WRITE:
362                 if (sc->cnt == sc->msg->len)
363                         rk_i2c_send_stop(sc);
364
365                 break;
366         case STATE_STOP:
367                 /* Disable stop bit */
368                 reg = RK_I2C_READ(sc, RK_I2C_CON);
369                 reg &= ~RK_I2C_CON_STOP;
370                 RK_I2C_WRITE(sc, RK_I2C_CON, reg);
371
372                 sc->transfer_done = 1;
373                 sc->state = STATE_IDLE;
374                 break;
375         case STATE_IDLE:
376                 break;
377         }
378
379         wakeup(sc);
380         RK_I2C_UNLOCK(sc);
381 }
382
383 static int
384 rk_i2c_transfer(device_t dev, struct iic_msg *msgs, uint32_t nmsgs)
385 {
386         struct rk_i2c_softc *sc;
387         uint32_t reg;
388         int i, j, msgskip, err = 0;
389
390         sc = device_get_softc(dev);
391
392         while (sc->busy)
393                 mtx_sleep(sc, &sc->mtx, 0, "i2cbuswait", 0);
394
395         sc->busy = 1;
396
397         err = clk_enable(sc->pclk);
398         if (err != 0) {
399                 device_printf(dev, "cannot enable pclk clock\n");
400                 goto out;
401         }
402         err = clk_enable(sc->sclk);
403         if (err != 0) {
404                 device_printf(dev, "cannot enable i2c clock\n");
405                 goto out;
406         }
407
408         RK_I2C_LOCK(sc);
409
410         /* Clean stale interrupts */
411         RK_I2C_WRITE(sc, RK_I2C_IPD, RK_I2C_IPD_ALL);
412
413         for (i = 0; i < nmsgs; i += msgskip) {
414                 if (nmsgs - i >= 2 && !(msgs[i].flags & IIC_M_RD) &&
415                   msgs[i + 1].flags & IIC_M_RD && msgs[i].len <= 4) {
416                         sc->mode = RK_I2C_CON_MODE_RRX;
417                         msgskip = 2;
418                         sc->msg = &msgs[i + 1];
419
420                         /* Write slave address */
421                         reg = msgs[i].slave | RK_I2C_MRXADDR_VALID(0);
422                         RK_I2C_WRITE(sc, RK_I2C_MRXADDR, reg);
423                         /* Write slave register address */
424                         for (j = 0, reg = 0; j < msgs[i].len; j++) {
425                                 reg |= (msgs[i].buf[j] & 0xff) << (j * 8);
426                                 reg |= RK_I2C_MRXADDR_VALID(j);
427                         }
428
429                         RK_I2C_WRITE(sc, RK_I2C_MRXRADDR, reg);
430                 } else {
431                         if (msgs[i].flags & IIC_M_RD) {
432                                 sc->mode = RK_I2C_CON_MODE_RX;
433                                 msgs[i].slave |= LSB;
434                         }
435                         else {
436                                 sc->mode = RK_I2C_CON_MODE_TX;
437                                 msgs[i].slave &= ~LSB;
438                         }
439                         msgskip = 1;
440                         sc->msg = &msgs[i];
441                 }
442
443                 sc->transfer_done = 0;
444                 sc->cnt = 0;
445                 sc->state = STATE_IDLE;
446                 rk_i2c_send_start(sc);
447
448                 while (err == 0 && sc->transfer_done != 1) {
449                         err = msleep(sc, &sc->mtx, 0, "rk_i2c", 10 * hz);
450                 }
451         }
452
453         /* Disable the module and interrupts */
454         RK_I2C_WRITE(sc, RK_I2C_CON, 0);
455         RK_I2C_WRITE(sc, RK_I2C_IEN, 0);
456
457         sc->busy = 0;
458
459         RK_I2C_UNLOCK(sc);
460
461         err = clk_disable(sc->pclk);
462         if (err != 0) {
463                 device_printf(dev, "cannot enable pclk clock\n");
464                 goto out;
465         }
466         err = clk_disable(sc->sclk);
467         if (err != 0) {
468                 device_printf(dev, "cannot enable i2c clock\n");
469                 goto out;
470         }
471
472 out:
473         return (err);
474 }
475
476 static int
477 rk_i2c_probe(device_t dev)
478 {
479
480         if (!ofw_bus_status_okay(dev))
481                 return (ENXIO);
482         if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
483                 return (ENXIO);
484
485         device_set_desc(dev, "RockChip I2C");
486         return (BUS_PROBE_DEFAULT);
487 }
488
489 static int
490 rk_i2c_attach(device_t dev)
491 {
492         struct rk_i2c_softc *sc;
493         int error;
494
495         sc = device_get_softc(dev);
496         sc->dev = dev;
497
498         mtx_init(&sc->mtx, device_get_nameunit(dev), "rk_i2c", MTX_DEF);
499
500         if (bus_alloc_resources(dev, rk_i2c_spec, sc->res) != 0) {
501                 device_printf(dev, "cannot allocate resources for device\n");
502                 error = ENXIO;
503                 goto fail;
504         }
505
506         if (bus_setup_intr(dev, sc->res[1],
507             INTR_TYPE_MISC | INTR_MPSAFE, NULL, rk_i2c_intr, sc,
508             &sc->intrhand)) {
509                 bus_release_resources(dev, rk_i2c_spec, sc->res);
510                 device_printf(dev, "cannot setup interrupt handler\n");
511                 return (ENXIO);
512         }
513
514         clk_set_assigned(dev, ofw_bus_get_node(dev));
515
516         /* Activate the module clocks. */
517         error = clk_get_by_ofw_name(dev, 0, "i2c", &sc->sclk);
518         if (error != 0) {
519                 device_printf(dev, "cannot get i2c clock\n");
520                 goto fail;
521         }
522         error = clk_get_by_ofw_name(dev, 0, "pclk", &sc->pclk);
523         if (error != 0) {
524                 device_printf(dev, "cannot get pclk clock\n");
525                 goto fail;
526         }
527
528         sc->iicbus = device_add_child(dev, "iicbus", -1);
529         if (sc->iicbus == NULL) {
530                 device_printf(dev, "cannot add iicbus child device\n");
531                 error = ENXIO;
532                 goto fail;
533         }
534
535         bus_generic_attach(dev);
536
537         return (0);
538
539 fail:
540         if (rk_i2c_detach(dev) != 0)
541                 device_printf(dev, "Failed to detach\n");
542         return (error);
543 }
544
545 static int
546 rk_i2c_detach(device_t dev)
547 {
548         struct rk_i2c_softc *sc;
549         int error;
550
551         sc = device_get_softc(dev);
552
553         if ((error = bus_generic_detach(dev)) != 0)
554                 return (error);
555
556         if (sc->iicbus != NULL)
557                 if ((error = device_delete_child(dev, sc->iicbus)) != 0)
558                         return (error);
559
560         if (sc->sclk != NULL)
561                 clk_release(sc->sclk);
562         if (sc->pclk != NULL)
563                 clk_release(sc->pclk);
564
565         if (sc->intrhand != NULL)
566                 bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand);
567
568         bus_release_resources(dev, rk_i2c_spec, sc->res);
569
570         mtx_destroy(&sc->mtx);
571
572         return (0);
573 }
574
575 static phandle_t
576 rk_i2c_get_node(device_t bus, device_t dev)
577 {
578
579         return ofw_bus_get_node(bus);
580 }
581
582 static device_method_t rk_i2c_methods[] = {
583         DEVMETHOD(device_probe,         rk_i2c_probe),
584         DEVMETHOD(device_attach,        rk_i2c_attach),
585         DEVMETHOD(device_detach,        rk_i2c_detach),
586
587         /* OFW methods */
588         DEVMETHOD(ofw_bus_get_node,             rk_i2c_get_node),
589
590         DEVMETHOD(iicbus_callback,      iicbus_null_callback),
591         DEVMETHOD(iicbus_reset,         rk_i2c_reset),
592         DEVMETHOD(iicbus_transfer,      rk_i2c_transfer),
593
594         DEVMETHOD_END
595 };
596
597 static driver_t rk_i2c_driver = {
598         "rk_i2c",
599         rk_i2c_methods,
600         sizeof(struct rk_i2c_softc),
601 };
602
603 static devclass_t rk_i2c_devclass;
604
605 EARLY_DRIVER_MODULE(rk_i2c, simplebus, rk_i2c_driver, rk_i2c_devclass, 0, 0,
606     BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
607 EARLY_DRIVER_MODULE(ofw_iicbus, rk_i2c, ofw_iicbus_driver, ofw_iicbus_devclass,
608     0, 0, BUS_PASS_INTERRUPT + BUS_PASS_ORDER_LATE);
609 MODULE_DEPEND(rk_i2c, iicbus, 1, 1, 1);
610 MODULE_DEPEND(rk_i2c, ofw_iicbus, 1, 1, 1);
611 MODULE_VERSION(rk_i2c, 1);