0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/arm-smccc.h>
0017 #include <linux/clk.h>
0018 #include <linux/dma-mapping.h>
0019 #include <linux/gpio/consumer.h>
0020 #include <linux/of_platform.h>
0021 #include <linux/platform_device.h>
0022 #include <linux/platform_data/atmel.h>
0023 #include <linux/io.h>
0024 #include <linux/kernel.h>
0025 #include <linux/module.h>
0026 #include <linux/mfd/syscon.h>
0027 #include <linux/regmap.h>
0028 #include <linux/usb.h>
0029 #include <linux/usb/hcd.h>
0030 #include <soc/at91/atmel-sfr.h>
0031
0032 #include "ohci.h"
0033
0034 #define valid_port(index) ((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
0035 #define at91_for_each_port(index) \
0036 for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)
0037
0038
0039 #define hcd_to_ohci_at91_priv(h) \
0040 ((struct ohci_at91_priv *)hcd_to_ohci(h)->priv)
0041
0042 #define AT91_MAX_USBH_PORTS 3
0043 struct at91_usbh_data {
0044 struct gpio_desc *vbus_pin[AT91_MAX_USBH_PORTS];
0045 struct gpio_desc *overcurrent_pin[AT91_MAX_USBH_PORTS];
0046 u8 ports;
0047 u8 overcurrent_supported;
0048 u8 overcurrent_status[AT91_MAX_USBH_PORTS];
0049 u8 overcurrent_changed[AT91_MAX_USBH_PORTS];
0050 };
0051
0052 struct ohci_at91_priv {
0053 struct clk *iclk;
0054 struct clk *fclk;
0055 struct clk *hclk;
0056 bool clocked;
0057 bool wakeup;
0058 struct regmap *sfr_regmap;
0059 u32 suspend_smc_id;
0060 };
0061
0062
0063 #define DRIVER_DESC "OHCI Atmel driver"
0064
0065 static const char hcd_name[] = "ohci-atmel";
0066
0067 static struct hc_driver __read_mostly ohci_at91_hc_driver;
0068
0069 static const struct ohci_driver_overrides ohci_at91_drv_overrides __initconst = {
0070 .extra_priv_size = sizeof(struct ohci_at91_priv),
0071 };
0072
0073
0074
0075 static void at91_start_clock(struct ohci_at91_priv *ohci_at91)
0076 {
0077 if (ohci_at91->clocked)
0078 return;
0079
0080 clk_set_rate(ohci_at91->fclk, 48000000);
0081 clk_prepare_enable(ohci_at91->hclk);
0082 clk_prepare_enable(ohci_at91->iclk);
0083 clk_prepare_enable(ohci_at91->fclk);
0084 ohci_at91->clocked = true;
0085 }
0086
0087 static void at91_stop_clock(struct ohci_at91_priv *ohci_at91)
0088 {
0089 if (!ohci_at91->clocked)
0090 return;
0091
0092 clk_disable_unprepare(ohci_at91->fclk);
0093 clk_disable_unprepare(ohci_at91->iclk);
0094 clk_disable_unprepare(ohci_at91->hclk);
0095 ohci_at91->clocked = false;
0096 }
0097
0098 static void at91_start_hc(struct platform_device *pdev)
0099 {
0100 struct usb_hcd *hcd = platform_get_drvdata(pdev);
0101 struct ohci_regs __iomem *regs = hcd->regs;
0102 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
0103
0104 dev_dbg(&pdev->dev, "start\n");
0105
0106
0107
0108
0109 at91_start_clock(ohci_at91);
0110
0111
0112
0113
0114 writel(0, ®s->control);
0115 }
0116
0117 static void at91_stop_hc(struct platform_device *pdev)
0118 {
0119 struct usb_hcd *hcd = platform_get_drvdata(pdev);
0120 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
0121
0122 dev_dbg(&pdev->dev, "stop\n");
0123
0124
0125
0126
0127 usb_hcd_platform_shutdown(pdev);
0128
0129
0130
0131
0132 at91_stop_clock(ohci_at91);
0133 }
0134
0135
0136
0137
0138 static void usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
0139
0140 static u32 at91_dt_suspend_smc(struct device *dev)
0141 {
0142 u32 suspend_smc_id;
0143
0144 if (!dev->of_node)
0145 return 0;
0146
0147 if (of_property_read_u32(dev->of_node, "microchip,suspend-smc-id", &suspend_smc_id))
0148 return 0;
0149
0150 return suspend_smc_id;
0151 }
0152
0153 static struct regmap *at91_dt_syscon_sfr(void)
0154 {
0155 struct regmap *regmap;
0156
0157 regmap = syscon_regmap_lookup_by_compatible("atmel,sama5d2-sfr");
0158 if (IS_ERR(regmap)) {
0159 regmap = syscon_regmap_lookup_by_compatible("microchip,sam9x60-sfr");
0160 if (IS_ERR(regmap))
0161 regmap = NULL;
0162 }
0163
0164 return regmap;
0165 }
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182 static int usb_hcd_at91_probe(const struct hc_driver *driver,
0183 struct platform_device *pdev)
0184 {
0185 struct at91_usbh_data *board;
0186 struct ohci_hcd *ohci;
0187 int retval;
0188 struct usb_hcd *hcd;
0189 struct ohci_at91_priv *ohci_at91;
0190 struct device *dev = &pdev->dev;
0191 struct resource *res;
0192 int irq;
0193
0194 irq = platform_get_irq(pdev, 0);
0195 if (irq < 0) {
0196 dev_dbg(dev, "hcd probe: missing irq resource\n");
0197 return irq;
0198 }
0199
0200 hcd = usb_create_hcd(driver, dev, "at91");
0201 if (!hcd)
0202 return -ENOMEM;
0203 ohci_at91 = hcd_to_ohci_at91_priv(hcd);
0204
0205 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0206 hcd->regs = devm_ioremap_resource(dev, res);
0207 if (IS_ERR(hcd->regs)) {
0208 retval = PTR_ERR(hcd->regs);
0209 goto err;
0210 }
0211 hcd->rsrc_start = res->start;
0212 hcd->rsrc_len = resource_size(res);
0213
0214 ohci_at91->iclk = devm_clk_get(dev, "ohci_clk");
0215 if (IS_ERR(ohci_at91->iclk)) {
0216 dev_err(dev, "failed to get ohci_clk\n");
0217 retval = PTR_ERR(ohci_at91->iclk);
0218 goto err;
0219 }
0220 ohci_at91->fclk = devm_clk_get(dev, "uhpck");
0221 if (IS_ERR(ohci_at91->fclk)) {
0222 dev_err(dev, "failed to get uhpck\n");
0223 retval = PTR_ERR(ohci_at91->fclk);
0224 goto err;
0225 }
0226 ohci_at91->hclk = devm_clk_get(dev, "hclk");
0227 if (IS_ERR(ohci_at91->hclk)) {
0228 dev_err(dev, "failed to get hclk\n");
0229 retval = PTR_ERR(ohci_at91->hclk);
0230 goto err;
0231 }
0232
0233 ohci_at91->suspend_smc_id = at91_dt_suspend_smc(dev);
0234 if (!ohci_at91->suspend_smc_id) {
0235 dev_dbg(dev, "failed to find sfr suspend smc id, using regmap\n");
0236 ohci_at91->sfr_regmap = at91_dt_syscon_sfr();
0237 if (!ohci_at91->sfr_regmap)
0238 dev_dbg(dev, "failed to find sfr node\n");
0239 }
0240
0241 board = hcd->self.controller->platform_data;
0242 ohci = hcd_to_ohci(hcd);
0243 ohci->num_ports = board->ports;
0244 at91_start_hc(pdev);
0245
0246
0247
0248
0249
0250 ohci->hc_control = OHCI_CTRL_RWC;
0251
0252 retval = usb_add_hcd(hcd, irq, IRQF_SHARED);
0253 if (retval == 0) {
0254 device_wakeup_enable(hcd->self.controller);
0255 return retval;
0256 }
0257
0258
0259 at91_stop_hc(pdev);
0260
0261 err:
0262 usb_put_hcd(hcd);
0263 return retval;
0264 }
0265
0266
0267
0268
0269
0270
0271
0272
0273
0274
0275
0276
0277
0278
0279
0280 static void usb_hcd_at91_remove(struct usb_hcd *hcd,
0281 struct platform_device *pdev)
0282 {
0283 usb_remove_hcd(hcd);
0284 at91_stop_hc(pdev);
0285 usb_put_hcd(hcd);
0286 }
0287
0288
0289 static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
0290 {
0291 if (!valid_port(port))
0292 return;
0293
0294 gpiod_set_value(pdata->vbus_pin[port], enable);
0295 }
0296
0297 static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
0298 {
0299 if (!valid_port(port))
0300 return -EINVAL;
0301
0302 return gpiod_get_value(pdata->vbus_pin[port]);
0303 }
0304
0305
0306
0307
0308 static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
0309 {
0310 struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
0311 int length = ohci_hub_status_data(hcd, buf);
0312 int port;
0313
0314 at91_for_each_port(port) {
0315 if (pdata->overcurrent_changed[port]) {
0316 if (!length)
0317 length = 1;
0318 buf[0] |= 1 << (port + 1);
0319 }
0320 }
0321
0322 return length;
0323 }
0324
0325 static int ohci_at91_port_suspend(struct ohci_at91_priv *ohci_at91, u8 set)
0326 {
0327 struct regmap *regmap = ohci_at91->sfr_regmap;
0328 u32 regval;
0329 int ret;
0330
0331 if (ohci_at91->suspend_smc_id) {
0332 struct arm_smccc_res res;
0333
0334 arm_smccc_smc(ohci_at91->suspend_smc_id, set, 0, 0, 0, 0, 0, 0, &res);
0335 if (res.a0)
0336 return -EINVAL;
0337 } else if (regmap) {
0338 ret = regmap_read(regmap, AT91_SFR_OHCIICR, ®val);
0339 if (ret)
0340 return ret;
0341
0342 if (set)
0343 regval |= AT91_OHCIICR_USB_SUSPEND;
0344 else
0345 regval &= ~AT91_OHCIICR_USB_SUSPEND;
0346
0347 regmap_write(regmap, AT91_SFR_OHCIICR, regval);
0348 }
0349
0350 return 0;
0351 }
0352
0353
0354
0355
0356 static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
0357 u16 wIndex, char *buf, u16 wLength)
0358 {
0359 struct at91_usbh_data *pdata = dev_get_platdata(hcd->self.controller);
0360 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
0361 struct usb_hub_descriptor *desc;
0362 int ret = -EINVAL;
0363 u32 *data = (u32 *)buf;
0364
0365 dev_dbg(hcd->self.controller,
0366 "ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
0367 hcd, typeReq, wValue, wIndex, buf, wLength);
0368
0369 wIndex--;
0370
0371 switch (typeReq) {
0372 case SetPortFeature:
0373 switch (wValue) {
0374 case USB_PORT_FEAT_POWER:
0375 dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
0376 if (valid_port(wIndex)) {
0377 ohci_at91_usb_set_power(pdata, wIndex, 1);
0378 ret = 0;
0379 }
0380
0381 goto out;
0382
0383 case USB_PORT_FEAT_SUSPEND:
0384 dev_dbg(hcd->self.controller, "SetPortFeat: SUSPEND\n");
0385 if (valid_port(wIndex)) {
0386 ohci_at91_port_suspend(ohci_at91, 1);
0387 return 0;
0388 }
0389 break;
0390 }
0391 break;
0392
0393 case ClearPortFeature:
0394 switch (wValue) {
0395 case USB_PORT_FEAT_C_OVER_CURRENT:
0396 dev_dbg(hcd->self.controller,
0397 "ClearPortFeature: C_OVER_CURRENT\n");
0398
0399 if (valid_port(wIndex)) {
0400 pdata->overcurrent_changed[wIndex] = 0;
0401 pdata->overcurrent_status[wIndex] = 0;
0402 }
0403
0404 goto out;
0405
0406 case USB_PORT_FEAT_OVER_CURRENT:
0407 dev_dbg(hcd->self.controller,
0408 "ClearPortFeature: OVER_CURRENT\n");
0409
0410 if (valid_port(wIndex))
0411 pdata->overcurrent_status[wIndex] = 0;
0412
0413 goto out;
0414
0415 case USB_PORT_FEAT_POWER:
0416 dev_dbg(hcd->self.controller,
0417 "ClearPortFeature: POWER\n");
0418
0419 if (valid_port(wIndex)) {
0420 ohci_at91_usb_set_power(pdata, wIndex, 0);
0421 return 0;
0422 }
0423 break;
0424
0425 case USB_PORT_FEAT_SUSPEND:
0426 dev_dbg(hcd->self.controller, "ClearPortFeature: SUSPEND\n");
0427 if (valid_port(wIndex)) {
0428 ohci_at91_port_suspend(ohci_at91, 0);
0429 return 0;
0430 }
0431 break;
0432 }
0433 break;
0434 }
0435
0436 ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
0437 if (ret)
0438 goto out;
0439
0440 switch (typeReq) {
0441 case GetHubDescriptor:
0442
0443
0444
0445 desc = (struct usb_hub_descriptor *)buf;
0446
0447 dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
0448 desc->wHubCharacteristics);
0449
0450
0451
0452
0453
0454 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
0455 desc->wHubCharacteristics |=
0456 cpu_to_le16(HUB_CHAR_INDV_PORT_LPSM);
0457
0458 if (pdata->overcurrent_supported) {
0459 desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
0460 desc->wHubCharacteristics |=
0461 cpu_to_le16(HUB_CHAR_INDV_PORT_OCPM);
0462 }
0463
0464 dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
0465 desc->wHubCharacteristics);
0466
0467 return ret;
0468
0469 case GetPortStatus:
0470
0471
0472 dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);
0473
0474 if (valid_port(wIndex)) {
0475 if (!ohci_at91_usb_get_power(pdata, wIndex))
0476 *data &= ~cpu_to_le32(RH_PS_PPS);
0477
0478 if (pdata->overcurrent_changed[wIndex])
0479 *data |= cpu_to_le32(RH_PS_OCIC);
0480
0481 if (pdata->overcurrent_status[wIndex])
0482 *data |= cpu_to_le32(RH_PS_POCI);
0483 }
0484 }
0485
0486 out:
0487 return ret;
0488 }
0489
0490
0491
0492 static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
0493 {
0494 struct platform_device *pdev = data;
0495 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
0496 int val, port;
0497
0498
0499
0500 at91_for_each_port(port) {
0501 if (gpiod_to_irq(pdata->overcurrent_pin[port]) == irq)
0502 break;
0503 }
0504
0505 if (port == AT91_MAX_USBH_PORTS) {
0506 dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
0507 return IRQ_HANDLED;
0508 }
0509
0510 val = gpiod_get_value(pdata->overcurrent_pin[port]);
0511
0512
0513
0514
0515 if (!val) {
0516 ohci_at91_usb_set_power(pdata, port, 0);
0517 pdata->overcurrent_status[port] = 1;
0518 pdata->overcurrent_changed[port] = 1;
0519 }
0520
0521 dev_dbg(& pdev->dev, "overcurrent situation %s\n",
0522 val ? "exited" : "notified");
0523
0524 return IRQ_HANDLED;
0525 }
0526
0527 static const struct of_device_id at91_ohci_dt_ids[] = {
0528 { .compatible = "atmel,at91rm9200-ohci" },
0529 { }
0530 };
0531
0532 MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);
0533
0534
0535
0536 static int ohci_hcd_at91_drv_probe(struct platform_device *pdev)
0537 {
0538 struct device_node *np = pdev->dev.of_node;
0539 struct at91_usbh_data *pdata;
0540 int i;
0541 int ret;
0542 int err;
0543 u32 ports;
0544
0545
0546
0547
0548
0549 ret = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
0550 if (ret)
0551 return ret;
0552
0553 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
0554 if (!pdata)
0555 return -ENOMEM;
0556
0557 pdev->dev.platform_data = pdata;
0558
0559 if (!of_property_read_u32(np, "num-ports", &ports))
0560 pdata->ports = ports;
0561
0562 at91_for_each_port(i) {
0563 if (i >= pdata->ports)
0564 break;
0565
0566 pdata->vbus_pin[i] =
0567 devm_gpiod_get_index_optional(&pdev->dev, "atmel,vbus",
0568 i, GPIOD_OUT_HIGH);
0569 if (IS_ERR(pdata->vbus_pin[i])) {
0570 err = PTR_ERR(pdata->vbus_pin[i]);
0571 dev_err(&pdev->dev, "unable to claim gpio \"vbus\": %d\n", err);
0572 continue;
0573 }
0574 }
0575
0576 at91_for_each_port(i) {
0577 if (i >= pdata->ports)
0578 break;
0579
0580 pdata->overcurrent_pin[i] =
0581 devm_gpiod_get_index_optional(&pdev->dev, "atmel,oc",
0582 i, GPIOD_IN);
0583 if (!pdata->overcurrent_pin[i])
0584 continue;
0585 if (IS_ERR(pdata->overcurrent_pin[i])) {
0586 err = PTR_ERR(pdata->overcurrent_pin[i]);
0587 dev_err(&pdev->dev, "unable to claim gpio \"overcurrent\": %d\n", err);
0588 continue;
0589 }
0590
0591 ret = devm_request_irq(&pdev->dev,
0592 gpiod_to_irq(pdata->overcurrent_pin[i]),
0593 ohci_hcd_at91_overcurrent_irq,
0594 IRQF_SHARED,
0595 "ohci_overcurrent", pdev);
0596 if (ret)
0597 dev_info(&pdev->dev, "failed to request gpio \"overcurrent\" IRQ\n");
0598 }
0599
0600 device_init_wakeup(&pdev->dev, 1);
0601 return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
0602 }
0603
0604 static int ohci_hcd_at91_drv_remove(struct platform_device *pdev)
0605 {
0606 struct at91_usbh_data *pdata = dev_get_platdata(&pdev->dev);
0607 int i;
0608
0609 if (pdata) {
0610 at91_for_each_port(i)
0611 ohci_at91_usb_set_power(pdata, i, 0);
0612 }
0613
0614 device_init_wakeup(&pdev->dev, 0);
0615 usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
0616 return 0;
0617 }
0618
0619 static int __maybe_unused
0620 ohci_hcd_at91_drv_suspend(struct device *dev)
0621 {
0622 struct usb_hcd *hcd = dev_get_drvdata(dev);
0623 struct ohci_hcd *ohci = hcd_to_ohci(hcd);
0624 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
0625 int ret;
0626
0627
0628
0629
0630
0631 ohci_at91->wakeup = device_may_wakeup(dev)
0632 && !at91_suspend_entering_slow_clock();
0633
0634 if (ohci_at91->wakeup)
0635 enable_irq_wake(hcd->irq);
0636
0637 ret = ohci_suspend(hcd, ohci_at91->wakeup);
0638 if (ret) {
0639 if (ohci_at91->wakeup)
0640 disable_irq_wake(hcd->irq);
0641 return ret;
0642 }
0643
0644
0645
0646
0647
0648
0649
0650 if (!ohci_at91->wakeup) {
0651 ohci->rh_state = OHCI_RH_HALTED;
0652
0653
0654 (void) ohci_readl (ohci, &ohci->regs->control);
0655 msleep(1);
0656 ohci_at91_port_suspend(ohci_at91, 1);
0657 at91_stop_clock(ohci_at91);
0658 } else {
0659 ohci_at91_port_suspend(ohci_at91, 1);
0660 }
0661
0662 return ret;
0663 }
0664
0665 static int __maybe_unused
0666 ohci_hcd_at91_drv_resume(struct device *dev)
0667 {
0668 struct usb_hcd *hcd = dev_get_drvdata(dev);
0669 struct ohci_at91_priv *ohci_at91 = hcd_to_ohci_at91_priv(hcd);
0670
0671 ohci_at91_port_suspend(ohci_at91, 0);
0672
0673 if (ohci_at91->wakeup)
0674 disable_irq_wake(hcd->irq);
0675 else
0676 at91_start_clock(ohci_at91);
0677
0678 ohci_resume(hcd, false);
0679
0680 return 0;
0681 }
0682
0683 static SIMPLE_DEV_PM_OPS(ohci_hcd_at91_pm_ops, ohci_hcd_at91_drv_suspend,
0684 ohci_hcd_at91_drv_resume);
0685
0686 static struct platform_driver ohci_hcd_at91_driver = {
0687 .probe = ohci_hcd_at91_drv_probe,
0688 .remove = ohci_hcd_at91_drv_remove,
0689 .shutdown = usb_hcd_platform_shutdown,
0690 .driver = {
0691 .name = "at91_ohci",
0692 .pm = &ohci_hcd_at91_pm_ops,
0693 .of_match_table = at91_ohci_dt_ids,
0694 },
0695 };
0696
0697 static int __init ohci_at91_init(void)
0698 {
0699 if (usb_disabled())
0700 return -ENODEV;
0701
0702 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
0703 ohci_init_driver(&ohci_at91_hc_driver, &ohci_at91_drv_overrides);
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714 ohci_at91_hc_driver.hub_status_data = ohci_at91_hub_status_data;
0715 ohci_at91_hc_driver.hub_control = ohci_at91_hub_control;
0716
0717 return platform_driver_register(&ohci_hcd_at91_driver);
0718 }
0719 module_init(ohci_at91_init);
0720
0721 static void __exit ohci_at91_cleanup(void)
0722 {
0723 platform_driver_unregister(&ohci_hcd_at91_driver);
0724 }
0725 module_exit(ohci_at91_cleanup);
0726
0727 MODULE_DESCRIPTION(DRIVER_DESC);
0728 MODULE_LICENSE("GPL");
0729 MODULE_ALIAS("platform:at91_ohci");