1*500d9e35Sjmcneill /* $NetBSD: cdnsiic.c,v 1.1 2022/11/05 17:31:37 jmcneill Exp $ */
2*500d9e35Sjmcneill
3*500d9e35Sjmcneill /*-
4*500d9e35Sjmcneill * Copyright (c) 2022 Jared McNeill <jmcneill@invisible.ca>
5*500d9e35Sjmcneill * All rights reserved.
6*500d9e35Sjmcneill *
7*500d9e35Sjmcneill * Redistribution and use in source and binary forms, with or without
8*500d9e35Sjmcneill * modification, are permitted provided that the following conditions
9*500d9e35Sjmcneill * are met:
10*500d9e35Sjmcneill * 1. Redistributions of source code must retain the above copyright
11*500d9e35Sjmcneill * notice, this list of conditions and the following disclaimer.
12*500d9e35Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
13*500d9e35Sjmcneill * notice, this list of conditions and the following disclaimer in the
14*500d9e35Sjmcneill * documentation and/or other materials provided with the distribution.
15*500d9e35Sjmcneill *
16*500d9e35Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*500d9e35Sjmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*500d9e35Sjmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*500d9e35Sjmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*500d9e35Sjmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*500d9e35Sjmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*500d9e35Sjmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*500d9e35Sjmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*500d9e35Sjmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*500d9e35Sjmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*500d9e35Sjmcneill * POSSIBILITY OF SUCH DAMAGE.
27*500d9e35Sjmcneill */
28*500d9e35Sjmcneill
29*500d9e35Sjmcneill /*
30*500d9e35Sjmcneill * Cadence I2C controller
31*500d9e35Sjmcneill */
32*500d9e35Sjmcneill
33*500d9e35Sjmcneill #include <sys/cdefs.h>
34*500d9e35Sjmcneill
35*500d9e35Sjmcneill __KERNEL_RCSID(0, "$NetBSD: cdnsiic.c,v 1.1 2022/11/05 17:31:37 jmcneill Exp $");
36*500d9e35Sjmcneill
37*500d9e35Sjmcneill #include <sys/param.h>
38*500d9e35Sjmcneill #include <sys/bus.h>
39*500d9e35Sjmcneill #include <sys/device.h>
40*500d9e35Sjmcneill #include <sys/intr.h>
41*500d9e35Sjmcneill #include <sys/systm.h>
42*500d9e35Sjmcneill #include <sys/time.h>
43*500d9e35Sjmcneill #include <sys/kmem.h>
44*500d9e35Sjmcneill
45*500d9e35Sjmcneill #include <dev/clk/clk.h>
46*500d9e35Sjmcneill #include <dev/i2c/i2cvar.h>
47*500d9e35Sjmcneill
48*500d9e35Sjmcneill #include <dev/ic/cdnsiicvar.h>
49*500d9e35Sjmcneill
50*500d9e35Sjmcneill /* From Zynq-7000 SoC Technical Reference Manual, "Supports 16-byte FIFO" */
51*500d9e35Sjmcneill #define FIFO_DEPTH 16
52*500d9e35Sjmcneill
53*500d9e35Sjmcneill /* Poll timeout, in microseconds. */
54*500d9e35Sjmcneill #define POLL_TIMEOUT 10000
55*500d9e35Sjmcneill
56*500d9e35Sjmcneill #define CR_REG 0x00
57*500d9e35Sjmcneill #define CR_DIV_A __BITS(15,14)
58*500d9e35Sjmcneill #define CR_DIV_B __BITS(13,8)
59*500d9e35Sjmcneill #define CR_CLR_FIFO __BIT(6)
60*500d9e35Sjmcneill #define CR_HOLD __BIT(4)
61*500d9e35Sjmcneill #define CR_ACKEN __BIT(3)
62*500d9e35Sjmcneill #define CR_NEA __BIT(2)
63*500d9e35Sjmcneill #define CR_MS __BIT(1)
64*500d9e35Sjmcneill #define CR_RD_WR __BIT(0)
65*500d9e35Sjmcneill #define SR_REG 0x04
66*500d9e35Sjmcneill #define SR_TXDV __BIT(6)
67*500d9e35Sjmcneill #define SR_RXDV __BIT(5)
68*500d9e35Sjmcneill #define ADDR_REG 0x08
69*500d9e35Sjmcneill #define DATA_REG 0x0c
70*500d9e35Sjmcneill #define ISR_REG 0x10
71*500d9e35Sjmcneill #define ISR_ARB_LOST __BIT(9)
72*500d9e35Sjmcneill #define ISR_RX_UNF __BIT(7)
73*500d9e35Sjmcneill #define ISR_TX_OVR __BIT(6)
74*500d9e35Sjmcneill #define ISR_RX_OVR __BIT(5)
75*500d9e35Sjmcneill #define ISR_SLV_RDY __BIT(4)
76*500d9e35Sjmcneill #define ISR_TO __BIT(3)
77*500d9e35Sjmcneill #define ISR_NACK __BIT(2)
78*500d9e35Sjmcneill #define ISR_DATA __BIT(1)
79*500d9e35Sjmcneill #define ISR_COMP __BIT(0)
80*500d9e35Sjmcneill #define ISR_ERROR_MASK (ISR_ARB_LOST | ISR_TX_OVR | ISR_RX_OVR | ISR_NACK)
81*500d9e35Sjmcneill #define TRANS_SIZE_REG 0x14
82*500d9e35Sjmcneill #define SLV_PAUSE_REG 0x18
83*500d9e35Sjmcneill #define TIME_OUT_REG 0x1c
84*500d9e35Sjmcneill #define IMR_REG 0x20
85*500d9e35Sjmcneill #define IER_REG 0x24
86*500d9e35Sjmcneill #define IDR_REG 0x28
87*500d9e35Sjmcneill
88*500d9e35Sjmcneill #define RD4(sc, reg) \
89*500d9e35Sjmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
90*500d9e35Sjmcneill #define WR4(sc, reg, val) \
91*500d9e35Sjmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
92*500d9e35Sjmcneill
93*500d9e35Sjmcneill static int
cdnsiic_init(struct cdnsiic_softc * sc)94*500d9e35Sjmcneill cdnsiic_init(struct cdnsiic_softc *sc)
95*500d9e35Sjmcneill {
96*500d9e35Sjmcneill int diva, divb;
97*500d9e35Sjmcneill int diff, calc_bus_freq;
98*500d9e35Sjmcneill int best_diva, best_divb, best_diff;
99*500d9e35Sjmcneill u_int pclk;
100*500d9e35Sjmcneill
101*500d9e35Sjmcneill /*
102*500d9e35Sjmcneill * SCL frequency is calculated by the following formula:
103*500d9e35Sjmcneill *
104*500d9e35Sjmcneill * SCL Divisor = 22 * (divisor_a + 1) * (divisor_b + 1)
105*500d9e35Sjmcneill * SCL = PCLK / SCLK Divisor
106*500d9e35Sjmcneill */
107*500d9e35Sjmcneill
108*500d9e35Sjmcneill pclk = clk_get_rate(sc->sc_pclk);
109*500d9e35Sjmcneill best_diff = sc->sc_bus_freq;
110*500d9e35Sjmcneill best_diva = best_divb = 0;
111*500d9e35Sjmcneill
112*500d9e35Sjmcneill for (diva = 0; diva <= 0x3; diva++) {
113*500d9e35Sjmcneill divb = howmany(pclk, 22 * sc->sc_bus_freq * (diva + 1)) - 1;
114*500d9e35Sjmcneill if (divb < 0 || divb > 0x3f) {
115*500d9e35Sjmcneill continue;
116*500d9e35Sjmcneill }
117*500d9e35Sjmcneill calc_bus_freq = pclk / (22 * (diva + 1) * (divb + 1));
118*500d9e35Sjmcneill diff = sc->sc_bus_freq - calc_bus_freq;
119*500d9e35Sjmcneill if (diff < best_diff) {
120*500d9e35Sjmcneill best_diff = diff;
121*500d9e35Sjmcneill best_diva = diva;
122*500d9e35Sjmcneill best_divb = divb;
123*500d9e35Sjmcneill }
124*500d9e35Sjmcneill }
125*500d9e35Sjmcneill if (best_diff == sc->sc_bus_freq) {
126*500d9e35Sjmcneill return ENXIO;
127*500d9e35Sjmcneill }
128*500d9e35Sjmcneill
129*500d9e35Sjmcneill WR4(sc, CR_REG,
130*500d9e35Sjmcneill __SHIFTIN(best_diva, CR_DIV_A) |
131*500d9e35Sjmcneill __SHIFTIN(best_divb, CR_DIV_B) |
132*500d9e35Sjmcneill CR_CLR_FIFO |
133*500d9e35Sjmcneill CR_ACKEN |
134*500d9e35Sjmcneill CR_NEA |
135*500d9e35Sjmcneill CR_MS);
136*500d9e35Sjmcneill WR4(sc, TIME_OUT_REG, 0xff);
137*500d9e35Sjmcneill
138*500d9e35Sjmcneill return 0;
139*500d9e35Sjmcneill }
140*500d9e35Sjmcneill
141*500d9e35Sjmcneill static int
cdnsiic_poll_fifo(struct cdnsiic_softc * sc,uint32_t sr_mask,uint32_t sr_maskval)142*500d9e35Sjmcneill cdnsiic_poll_fifo(struct cdnsiic_softc *sc, uint32_t sr_mask, uint32_t sr_maskval)
143*500d9e35Sjmcneill {
144*500d9e35Sjmcneill uint32_t sr_val, isr_val;
145*500d9e35Sjmcneill int retry = POLL_TIMEOUT;
146*500d9e35Sjmcneill
147*500d9e35Sjmcneill while (--retry > 0) {
148*500d9e35Sjmcneill sr_val = RD4(sc, SR_REG);
149*500d9e35Sjmcneill isr_val = RD4(sc, ISR_REG);
150*500d9e35Sjmcneill if ((isr_val & ISR_ERROR_MASK) != 0) {
151*500d9e35Sjmcneill return EIO;
152*500d9e35Sjmcneill }
153*500d9e35Sjmcneill if ((sr_val & sr_mask) == sr_maskval) {
154*500d9e35Sjmcneill return 0;
155*500d9e35Sjmcneill }
156*500d9e35Sjmcneill delay(1);
157*500d9e35Sjmcneill }
158*500d9e35Sjmcneill
159*500d9e35Sjmcneill return ETIMEDOUT;
160*500d9e35Sjmcneill }
161*500d9e35Sjmcneill
162*500d9e35Sjmcneill static int
cdnsiic_poll_transfer_complete(struct cdnsiic_softc * sc)163*500d9e35Sjmcneill cdnsiic_poll_transfer_complete(struct cdnsiic_softc *sc)
164*500d9e35Sjmcneill {
165*500d9e35Sjmcneill uint32_t val;
166*500d9e35Sjmcneill int retry = POLL_TIMEOUT;
167*500d9e35Sjmcneill
168*500d9e35Sjmcneill while (--retry > 0) {
169*500d9e35Sjmcneill val = RD4(sc, ISR_REG);
170*500d9e35Sjmcneill if ((val & ISR_COMP) != 0) {
171*500d9e35Sjmcneill return 0;
172*500d9e35Sjmcneill }
173*500d9e35Sjmcneill delay(1);
174*500d9e35Sjmcneill }
175*500d9e35Sjmcneill
176*500d9e35Sjmcneill return ETIMEDOUT;
177*500d9e35Sjmcneill }
178*500d9e35Sjmcneill
179*500d9e35Sjmcneill static int
cdnsiic_write(struct cdnsiic_softc * sc,i2c_addr_t addr,const uint8_t * data,size_t datalen,bool send_stop)180*500d9e35Sjmcneill cdnsiic_write(struct cdnsiic_softc *sc, i2c_addr_t addr,
181*500d9e35Sjmcneill const uint8_t *data, size_t datalen, bool send_stop)
182*500d9e35Sjmcneill {
183*500d9e35Sjmcneill uint32_t val;
184*500d9e35Sjmcneill u_int xferlen, fifo_space, n;
185*500d9e35Sjmcneill bool write_addr = true;
186*500d9e35Sjmcneill int error;
187*500d9e35Sjmcneill
188*500d9e35Sjmcneill if (datalen == 0 || datalen > 256) {
189*500d9e35Sjmcneill return EINVAL;
190*500d9e35Sjmcneill }
191*500d9e35Sjmcneill
192*500d9e35Sjmcneill val = RD4(sc, CR_REG);
193*500d9e35Sjmcneill val |= CR_CLR_FIFO;
194*500d9e35Sjmcneill val &= ~CR_RD_WR;
195*500d9e35Sjmcneill WR4(sc, CR_REG, val);
196*500d9e35Sjmcneill WR4(sc, ISR_REG, RD4(sc, ISR_REG));
197*500d9e35Sjmcneill
198*500d9e35Sjmcneill while (datalen > 0) {
199*500d9e35Sjmcneill fifo_space = FIFO_DEPTH - RD4(sc, TRANS_SIZE_REG);
200*500d9e35Sjmcneill xferlen = uimin(datalen, fifo_space);
201*500d9e35Sjmcneill for (n = 0; n < xferlen; n++, data++) {
202*500d9e35Sjmcneill WR4(sc, DATA_REG, *data);
203*500d9e35Sjmcneill }
204*500d9e35Sjmcneill if (write_addr) {
205*500d9e35Sjmcneill WR4(sc, ADDR_REG, addr);
206*500d9e35Sjmcneill write_addr = false;
207*500d9e35Sjmcneill }
208*500d9e35Sjmcneill datalen -= xferlen;
209*500d9e35Sjmcneill error = cdnsiic_poll_fifo(sc, SR_TXDV, 0);
210*500d9e35Sjmcneill if (error != 0) {
211*500d9e35Sjmcneill return error;
212*500d9e35Sjmcneill }
213*500d9e35Sjmcneill }
214*500d9e35Sjmcneill
215*500d9e35Sjmcneill return cdnsiic_poll_transfer_complete(sc);
216*500d9e35Sjmcneill }
217*500d9e35Sjmcneill
218*500d9e35Sjmcneill static int
cdnsiic_read(struct cdnsiic_softc * sc,i2c_addr_t addr,uint8_t * data,size_t datalen)219*500d9e35Sjmcneill cdnsiic_read(struct cdnsiic_softc *sc, i2c_addr_t addr,
220*500d9e35Sjmcneill uint8_t *data, size_t datalen)
221*500d9e35Sjmcneill {
222*500d9e35Sjmcneill uint32_t val;
223*500d9e35Sjmcneill int error;
224*500d9e35Sjmcneill
225*500d9e35Sjmcneill if (datalen == 0 || datalen > 255) {
226*500d9e35Sjmcneill return EINVAL;
227*500d9e35Sjmcneill }
228*500d9e35Sjmcneill
229*500d9e35Sjmcneill val = RD4(sc, CR_REG);
230*500d9e35Sjmcneill val |= CR_CLR_FIFO | CR_RD_WR;
231*500d9e35Sjmcneill WR4(sc, CR_REG, val);
232*500d9e35Sjmcneill WR4(sc, ISR_REG, RD4(sc, ISR_REG));
233*500d9e35Sjmcneill WR4(sc, TRANS_SIZE_REG, datalen);
234*500d9e35Sjmcneill WR4(sc, ADDR_REG, addr);
235*500d9e35Sjmcneill
236*500d9e35Sjmcneill while (datalen > 0) {
237*500d9e35Sjmcneill error = cdnsiic_poll_fifo(sc, SR_RXDV, SR_RXDV);
238*500d9e35Sjmcneill if (error != 0) {
239*500d9e35Sjmcneill return error;
240*500d9e35Sjmcneill }
241*500d9e35Sjmcneill *data = RD4(sc, DATA_REG) & 0xff;
242*500d9e35Sjmcneill data++;
243*500d9e35Sjmcneill datalen--;
244*500d9e35Sjmcneill }
245*500d9e35Sjmcneill
246*500d9e35Sjmcneill return cdnsiic_poll_transfer_complete(sc);
247*500d9e35Sjmcneill }
248*500d9e35Sjmcneill
249*500d9e35Sjmcneill static int
cdnsiic_exec(void * priv,i2c_op_t op,i2c_addr_t addr,const void * cmdbuf,size_t cmdlen,void * buf,size_t buflen,int flags)250*500d9e35Sjmcneill cdnsiic_exec(void *priv, i2c_op_t op, i2c_addr_t addr,
251*500d9e35Sjmcneill const void *cmdbuf, size_t cmdlen, void *buf, size_t buflen, int flags)
252*500d9e35Sjmcneill {
253*500d9e35Sjmcneill struct cdnsiic_softc * const sc = priv;
254*500d9e35Sjmcneill uint32_t val;
255*500d9e35Sjmcneill int error;
256*500d9e35Sjmcneill
257*500d9e35Sjmcneill val = RD4(sc, CR_REG);
258*500d9e35Sjmcneill WR4(sc, CR_REG, val | CR_HOLD);
259*500d9e35Sjmcneill
260*500d9e35Sjmcneill if (cmdlen > 0) {
261*500d9e35Sjmcneill error = cdnsiic_write(sc, addr, cmdbuf, cmdlen, false);
262*500d9e35Sjmcneill if (error != 0) {
263*500d9e35Sjmcneill goto done;
264*500d9e35Sjmcneill }
265*500d9e35Sjmcneill }
266*500d9e35Sjmcneill if (I2C_OP_READ_P(op)) {
267*500d9e35Sjmcneill error = cdnsiic_read(sc, addr, buf, buflen);
268*500d9e35Sjmcneill } else {
269*500d9e35Sjmcneill error = cdnsiic_write(sc, addr, buf, buflen, true);
270*500d9e35Sjmcneill }
271*500d9e35Sjmcneill
272*500d9e35Sjmcneill done:
273*500d9e35Sjmcneill val = RD4(sc, CR_REG);
274*500d9e35Sjmcneill WR4(sc, CR_REG, val & ~CR_HOLD);
275*500d9e35Sjmcneill
276*500d9e35Sjmcneill return error;
277*500d9e35Sjmcneill }
278*500d9e35Sjmcneill
279*500d9e35Sjmcneill int
cdnsiic_attach(struct cdnsiic_softc * sc)280*500d9e35Sjmcneill cdnsiic_attach(struct cdnsiic_softc *sc)
281*500d9e35Sjmcneill {
282*500d9e35Sjmcneill int error;
283*500d9e35Sjmcneill
284*500d9e35Sjmcneill aprint_naive("\n");
285*500d9e35Sjmcneill aprint_normal(": Cadence I2C (%u Hz)\n", sc->sc_bus_freq);
286*500d9e35Sjmcneill
287*500d9e35Sjmcneill error = cdnsiic_init(sc);
288*500d9e35Sjmcneill if (error != 0) {
289*500d9e35Sjmcneill return error;
290*500d9e35Sjmcneill }
291*500d9e35Sjmcneill
292*500d9e35Sjmcneill iic_tag_init(&sc->sc_ic);
293*500d9e35Sjmcneill sc->sc_ic.ic_cookie = sc;
294*500d9e35Sjmcneill sc->sc_ic.ic_exec = cdnsiic_exec;
295*500d9e35Sjmcneill
296*500d9e35Sjmcneill return 0;
297*500d9e35Sjmcneill }
298