source: rtems-libbsd/rtemsbsd/sys/dev/usb/controller/ohci_lpc32xx.c @ 3c967ca

55-freebsd-126-freebsd-12
Last change on this file since 3c967ca was 3c967ca, checked in by Sebastian Huber <sebastian.huber@…>, on 06/08/17 at 11:15:12

Use <sys/lock.h> provided by Newlib

  • Property mode set to 100755
File size: 13.8 KB
Line 
1/*-
2 * Copyright (c) 2011 Jakub Wojciech Klama <jceel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 */
27#include <machine/rtems-bsd-kernel-space.h>
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD$");
31
32#include <sys/stdint.h>
33#include <sys/stddef.h>
34#include <sys/param.h>
35#include <sys/queue.h>
36#include <sys/types.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/bus.h>
40#include <sys/module.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/condvar.h>
44#include <sys/sysctl.h>
45#include <sys/rman.h>
46#include <sys/sx.h>
47#include <rtems/bsd/sys/unistd.h>
48#include <sys/callout.h>
49#include <sys/malloc.h>
50#include <sys/priv.h>
51
52#include <sys/kdb.h>
53
54#include <bsp.h>
55#if defined(LIBBSP_ARM_LPC32XX_BSP_H)
56
57#ifdef BSP_USB_OTG_TRANSCEIVER_I2C_ADDR
58#include <dev/usb/usb_otg_transceiver.h>
59#endif /* BSP_USB_OTG_TRANSCEIVER_I2C_ADDR */
60
61#include <dev/usb/usb.h>
62#include <dev/usb/usbdi.h>
63
64#include <dev/usb/usb_core.h>
65#include <dev/usb/usb_busdma.h>
66#include <dev/usb/usb_process.h>
67#include <dev/usb/usb_util.h>
68
69#include <dev/usb/usb_controller.h>
70#include <dev/usb/usb_bus.h>
71#include <dev/usb/controller/ohci.h>
72#include <dev/usb/controller/ohcireg.h>
73
74#include <arm/lpc/lpcreg.h>
75#include <arm/lpc/lpcvar.h>
76
77#define I2C_START_BIT           (1 << 8)
78#define I2C_STOP_BIT            (1 << 9)
79#define I2C_READ                0x01
80#define I2C_WRITE               0x00
81#define DUMMY_BYTE              0x55
82
83#define lpc_otg_read_4(_sc, _reg)                                       \
84    bus_space_read_4(_sc->sc_io_tag, _sc->sc_io_hdl, _reg)
85#define lpc_otg_write_4(_sc, _reg, _value)                              \
86    bus_space_write_4(_sc->sc_io_tag, _sc->sc_io_hdl, _reg, _value)
87#define lpc_otg_wait_write_4(_sc, _wreg, _sreg, _value)                 \
88    do {                                                                \
89        lpc_otg_write_4(_sc, _wreg, _value);                            \
90        while ((lpc_otg_read_4(_sc, _sreg) & _value) != _value);        \
91    } while (0);
92
93static int lpc_ohci_probe(device_t dev);
94static int lpc_ohci_attach(device_t dev);
95static int lpc_ohci_detach(device_t dev);
96
97static void lpc_usb_module_enable(device_t dev, struct ohci_softc *);
98static void lpc_usb_module_disable(device_t dev, struct ohci_softc *);
99static void lpc_usb_pin_config(device_t dev, struct ohci_softc *);
100static void lpc_usb_host_clock_enable(device_t dev, struct ohci_softc *);
101static void lpc_otg_status_and_control(device_t dev, struct ohci_softc *);
102static rtems_interval lpc_usb_timeout_init(void);
103static bool lpc_usb_timeout_not_expired(rtems_interval start);
104static int lpc_otg_clk_ctrl(device_t dev, struct ohci_softc *sc, uint32_t otg_clk_ctrl);
105static int lpc_otg_i2c_wait_for_receive_fifo_not_empty(struct ohci_softc *sc);
106static int lpc_otg_i2c_wait_for_transaction_done(struct ohci_softc *sc);
107static int lpc_otg_i2c_read(const struct usb_otg_transceiver *self, uint8_t reg_addr, uint8_t *value);
108static int lpc_otg_i2c_write(const struct usb_otg_transceiver *self, uint8_t reg_addr, uint8_t value);
109
110static int
111lpc_ohci_probe(device_t dev)
112{
113
114        device_set_desc(dev, "LPC32x0 USB OHCI controller");
115        return (BUS_PROBE_DEFAULT);
116}
117
118static int
119lpc_ohci_attach(device_t dev)
120{
121        struct ohci_softc *sc = device_get_softc(dev);
122        int err;
123  int eno;
124        int rid;
125        int i = 0;
126        uint32_t usbctrl;
127        uint32_t otgstatus;
128
129        sc->sc_bus.parent = dev;
130        sc->sc_bus.devices = sc->sc_devices;
131        sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
132        sc->sc_bus.dma_bits = 32;
133
134        if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(dev),
135            &ohci_iterate_hw_softc))
136                return (ENOMEM);
137
138        rid = 0;
139        sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
140        if (!sc->sc_io_res) {
141                device_printf(dev, "cannot map OHCI register space\n");
142                goto fail;
143        }
144
145        sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
146        sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
147        sc->sc_io_size = rman_get_size(sc->sc_io_res);
148
149        rid = 0;
150        sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, RF_ACTIVE);
151        if (sc->sc_irq_res == NULL) {
152                device_printf(dev, "cannot allocate interrupt\n");
153                goto fail;
154        }
155
156        sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
157        if (!(sc->sc_bus.bdev))
158                goto fail;
159
160        device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
161        strlcpy(sc->sc_vendor, "NXP", sizeof(sc->sc_vendor));
162
163        err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
164            NULL, (void *)ohci_interrupt, sc, &sc->sc_intr_hdl);
165        if (err) {
166                sc->sc_intr_hdl = NULL;
167                goto fail;
168        }
169
170  lpc_usb_module_enable(dev, sc);
171
172  eno = lpc_otg_clk_ctrl(dev, sc, LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_I2C_EN);
173  if (eno != 0) {
174    goto fail;
175  }
176
177  lpc_usb_pin_config(dev, sc);
178
179#ifdef BSP_USB_OTG_TRANSCEIVER_I2C_ADDR
180  sc->sc_otg_trans.read = lpc_otg_i2c_read;
181  sc->sc_otg_trans.write = lpc_otg_i2c_write;
182  sc->sc_otg_trans.i2c_addr = BSP_USB_OTG_TRANSCEIVER_I2C_ADDR;
183  sc->sc_otg_trans.softc = sc;
184  eno = usb_otg_transceiver_init(&sc->sc_otg_trans);
185  if (eno != 0) {
186    goto fail;
187  }
188
189#ifdef BSP_USB_OTG_TRANSCEIVER_DUMP
190  usb_otg_transceiver_dump(&sc->sc_otg_trans);
191#endif /* BSP_USB_OTG_TRANSCEIVER_DUMP */
192
193  eno = usb_otg_transceiver_resume(&sc->sc_otg_trans);
194  if (eno != 0) {
195    goto fail;
196  }
197#endif /* BSP_USB_OTG_TRANSCEIVER_I2C_ADDR */
198
199  lpc_usb_host_clock_enable(dev, sc);
200
201  eno = lpc_otg_clk_ctrl( dev, sc,
202    LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_HOST_EN
203      | LPC_OTG_CLOCK_CTRL_I2C_EN | LPC_OTG_CLOCK_CTRL_OTG_EN
204  );
205  if (eno != 0) {
206    goto fail;
207  }
208
209  lpc_otg_status_and_control(dev, sc);
210
211#if defined(BSP_USB_OTG_TRANSCEIVER_I2C_ADDR) \
212  && defined(BSP_USB_OTG_TRANSCEIVER_VBUS)
213  eno = usb_otg_transceiver_set_vbus(
214    &sc->sc_otg_trans,
215    BSP_USB_OTG_TRANSCEIVER_VBUS
216  );
217  if (eno != 0) {
218    goto fail;
219  }
220#endif /* defined(BSP_USB_OTG_TRANSCEIVER_I2C_ADDR)
221    && defined(BSP_USB_OTG_TRANSCEIVER_VBUS) */
222
223#if defined(BSP_USB_OTG_TRANSCEIVER_I2C_ADDR) \
224  && defined(BSP_USB_OTG_TRANSCEIVER_DUMP)
225  usb_otg_transceiver_dump(&sc->sc_otg_trans);
226#endif /* defined(BSP_USB_OTG_TRANSCEIVER_I2C_ADDR)
227    && defined(BSP_USB_OTG_TRANSCEIVER_DUMP) */
228
229  eno = lpc_otg_clk_ctrl(dev, sc, LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_HOST_EN);
230  if (eno != 0) {
231    goto fail;
232  }
233
234        err = ohci_init(sc);
235        if (err)
236                goto fail;
237
238        err = device_probe_and_attach(sc->sc_bus.bdev);
239        if (err)
240                goto fail;
241
242        return (0);
243
244fail:
245        if (sc->sc_intr_hdl)
246                bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr_hdl);
247        if (sc->sc_irq_res)
248                bus_release_resource(dev, SYS_RES_IRQ, 0, sc->sc_irq_res);
249        if (sc->sc_io_res)
250                bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->sc_io_res);
251
252        return (ENXIO);
253}
254
255static int
256lpc_ohci_detach(device_t dev)
257{
258        return (0);
259}
260
261static void lpc_usb_module_enable(device_t dev, struct ohci_softc *sc)
262{
263  uint32_t usbctrl;
264
265  lpc_pwr_write(dev, LPC_CLKPWR_USBDIV_CTRL, 0xc);
266  lpc_pwr_write(dev, LPC_CLKPWR_USB_CTRL,
267      LPC_CLKPWR_USB_CTRL_SLAVE_HCLK |
268      LPC_CLKPWR_USB_CTRL_BUSKEEPER |
269      LPC_CLKPWR_USB_CTRL_CLK_EN1 |
270      LPC_CLKPWR_USB_CTRL_PLL_PDOWN |
271      LPC_CLKPWR_USB_CTRL_POSTDIV(1) |
272      LPC_CLKPWR_USB_CTRL_PREDIV(0) |
273      LPC_CLKPWR_USB_CTRL_FDBKDIV(192));
274  do {
275    usbctrl = lpc_pwr_read(dev, LPC_CLKPWR_USB_CTRL);
276  } while ((usbctrl & LPC_CLKPWR_USB_CTRL_PLL_LOCK) == 0);
277
278  usbctrl = lpc_pwr_read(dev, LPC_CLKPWR_USB_CTRL);
279  usbctrl |= LPC_CLKPWR_USB_CTRL_CLK_EN2;
280  lpc_pwr_write(dev, LPC_CLKPWR_USB_CTRL, usbctrl);
281}
282
283static void lpc_usb_module_disable(device_t dev, struct ohci_softc *sc)
284{
285  lpc_otg_write_4(sc, LPC_OTG_CLOCK_CTRL, 0x0);
286  lpc_pwr_write(dev, LPC_CLKPWR_USB_CTRL, LPC_CLKPWR_USB_CTRL_BUSKEEPER);
287}
288
289static void lpc_usb_pin_config(device_t dev, struct ohci_softc *sc)
290{
291}
292
293static void lpc_usb_host_clock_enable(device_t dev, struct ohci_softc *sc)
294{
295  uint32_t usbctrl;
296
297  usbctrl = lpc_pwr_read(dev, LPC_CLKPWR_USB_CTRL);
298  usbctrl |= LPC_CLKPWR_USB_CTRL_HOST_NEED_CLK_EN;
299  lpc_pwr_write(dev, LPC_CLKPWR_USB_CTRL, usbctrl);
300}
301
302static void lpc_otg_status_and_control(device_t dev, struct ohci_softc *sc)
303{
304  lpc_otg_write_4(sc, LPC_OTG_STATUS, 0x1);
305}
306
307static rtems_interval lpc_usb_timeout_init(void)
308{
309  return rtems_clock_get_ticks_since_boot();
310}
311
312static bool lpc_usb_timeout_not_expired(rtems_interval start)
313{
314  rtems_interval elapsed = rtems_clock_get_ticks_since_boot() - start;
315
316  return elapsed < (rtems_clock_get_ticks_per_second() / 10);
317}
318
319static int lpc_otg_clk_ctrl(device_t dev, struct ohci_softc *sc, uint32_t otg_clk_ctrl)
320{
321  rtems_interval start;
322  bool not_ok;
323
324  lpc_otg_write_4(sc, LPC_OTG_CLOCK_CTRL, otg_clk_ctrl);
325
326  start = lpc_usb_timeout_init();
327  while (
328    (not_ok = (lpc_otg_read_4(sc, LPC_OTG_CLOCK_STATUS) & otg_clk_ctrl) != otg_clk_ctrl)
329      && lpc_usb_timeout_not_expired(start)
330  ) {
331    /* Wait */
332  }
333
334  return not_ok ? EIO : 0;
335}
336
337static int lpc_otg_i2c_wait_for_receive_fifo_not_empty(struct ohci_softc *sc)
338{
339  rtems_interval start;
340  bool not_ok;
341
342  start = lpc_usb_timeout_init();
343  while (
344    (not_ok = (lpc_otg_read_4(sc, LPC_OTG_I2C_STATUS) & LPC_OTG_I2C_STATUS_RFE) != 0)
345      && lpc_usb_timeout_not_expired(start)
346  ) {
347    /* Wait */
348  }
349
350  return not_ok ? EIO : 0;
351}
352
353static int lpc_otg_i2c_wait_for_transaction_done(struct ohci_softc *sc)
354{
355  rtems_interval start;
356  bool not_ok;
357
358  start = lpc_usb_timeout_init();
359  while (
360    (not_ok = (lpc_otg_read_4(sc, LPC_OTG_I2C_STATUS) & LPC_OTG_I2C_STATUS_TDI) == 0)
361      && lpc_usb_timeout_not_expired(start)
362  ) {
363    /* Wait */
364  }
365
366  return not_ok ? EIO : 0;
367}
368
369static int lpc_otg_i2c_read(const struct usb_otg_transceiver *self, uint8_t reg_addr, uint8_t *value)
370{
371  struct ohci_softc *sc = (struct ohci_softc *)self->softc;
372  int eno;
373
374  lpc_otg_write_4(sc, LPC_OTG_I2C_CTRL, LPC_OTG_I2C_CTRL_SRST);
375  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, self->i2c_addr | I2C_START_BIT);
376  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, reg_addr);
377  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, self->i2c_addr | I2C_START_BIT | I2C_READ);
378  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, I2C_STOP_BIT);
379
380  eno = lpc_otg_i2c_wait_for_receive_fifo_not_empty(sc);
381
382  if (eno == 0) {
383    *value = (uint8_t)lpc_otg_read_4(sc, LPC_OTG_I2C_TXRX);
384  }
385
386  return eno;
387}
388
389static int lpc_otg_i2c_write(const struct usb_otg_transceiver *self, uint8_t reg_addr, uint8_t value)
390{
391  struct ohci_softc *sc = (struct ohci_softc *)self->softc;
392  int eno;
393
394  lpc_otg_write_4(sc, LPC_OTG_I2C_CTRL, LPC_OTG_I2C_CTRL_SRST);
395  lpc_otg_write_4(sc, LPC_OTG_I2C_STATUS, LPC_OTG_I2C_STATUS_TDI);
396  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, self->i2c_addr | I2C_START_BIT);
397  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, reg_addr);
398  lpc_otg_write_4(sc, LPC_OTG_I2C_TXRX, value | I2C_STOP_BIT);
399
400  eno = lpc_otg_i2c_wait_for_transaction_done(sc);
401
402  return eno;
403}
404
405static int ohci_lpc_otg_transceiver_suspend(device_t dev, struct ohci_softc *sc)
406{
407  int eno = 0;
408
409#ifdef BSP_USB_OTG_TRANSCEIVER_I2C_ADDR
410  if (eno == 0) {
411    eno = lpc_otg_clk_ctrl( dev, sc,
412      LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_HOST_EN | LPC_OTG_CLOCK_CTRL_I2C_EN
413    );
414  }
415
416  if (eno == 0) {
417    eno = usb_otg_transceiver_suspend(&sc->sc_otg_trans);
418  }
419
420#ifdef BSP_USB_OTG_TRANSCEIVER_DUMP
421  usb_otg_transceiver_dump(&sc->sc_otg_trans);
422#endif /* BSP_USB_OTG_TRANSCEIVER_DUMP */
423
424  if (eno == 0) {
425    eno = lpc_otg_clk_ctrl(dev, sc, LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_HOST_EN);
426  }
427#endif /* BSP_USB_OTG_TRANSCEIVER_I2C_ADDR */
428
429  return eno;
430}
431
432static int
433lpc_ohci_resume(device_t dev)
434{
435  struct ohci_softc *sc = device_get_softc(dev);
436  int eno = 0;
437
438#ifdef BSP_USB_OTG_TRANSCEIVER_I2C_ADDR
439  if (eno == 0) {
440    eno = lpc_otg_clk_ctrl( dev, sc,
441      LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_HOST_EN | LPC_OTG_CLOCK_CTRL_I2C_EN
442    );
443  }
444
445  if (eno == 0) {
446    eno = usb_otg_transceiver_resume(&sc->sc_otg_trans);
447  }
448
449#ifdef BSP_USB_OTG_TRANSCEIVER_VBUS
450  if (eno == 0) {
451    eno = usb_otg_transceiver_set_vbus(
452      &sc->sc_otg_trans,
453      BSP_USB_OTG_TRANSCEIVER_VBUS
454    );
455  }
456#endif /* BSP_USB_OTG_TRANSCEIVER_VBUS */
457
458#ifdef BSP_USB_OTG_TRANSCEIVER_DUMP
459  usb_otg_transceiver_dump(&sc->sc_otg_trans);
460#endif /* BSP_USB_OTG_TRANSCEIVER_DUMP */
461
462  if (eno == 0) {
463    eno = lpc_otg_clk_ctrl(dev, sc, LPC_OTG_CLOCK_CTRL_AHB_EN | LPC_OTG_CLOCK_CTRL_HOST_EN);
464  }
465#endif /* BSP_USB_OTG_TRANSCEIVER_I2C_ADDR */
466
467  if (eno == 0) {
468    eno = bus_generic_resume(dev);
469  }
470
471  return (eno);
472}
473
474static device_method_t lpc_ohci_methods[] = {
475        /* Device interface */
476        DEVMETHOD(device_probe,         lpc_ohci_probe),
477        DEVMETHOD(device_attach,        lpc_ohci_attach),
478        DEVMETHOD(device_detach,        lpc_ohci_detach),
479  DEVMETHOD(device_suspend, bus_generic_suspend),
480  DEVMETHOD(device_resume,  lpc_ohci_resume),
481        DEVMETHOD(device_shutdown,      bus_generic_shutdown),
482
483        /* Bus interface */
484        DEVMETHOD(bus_print_child,      bus_generic_print_child),
485        { 0, 0 }
486};
487
488static driver_t lpc_ohci_driver = {
489        "ohci",
490        lpc_ohci_methods,
491        sizeof(struct ohci_softc),
492};
493
494static devclass_t lpc_ohci_devclass;
495
496DRIVER_MODULE(ohci, nexus, lpc_ohci_driver, lpc_ohci_devclass, 0, 0);
497MODULE_DEPEND(ohci, usb, 1, 1, 1);
498
499#endif /* defined(LIBBSP_ARM_LPC32XX_BSP_H) */
Note: See TracBrowser for help on using the repository browser.