devices.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/arch/arm/mach-mmp/devices.c
  4. */
  5. #include <linux/init.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/dma-mapping.h>
  8. #include <linux/delay.h>
  9. #include <asm/irq.h>
  10. #include "irqs.h"
  11. #include "devices.h"
  12. #include <linux/soc/mmp/cputype.h>
  13. #include "regs-usb.h"
  14. int __init mmp_register_device(struct mmp_device_desc *desc,
  15. void *data, size_t size)
  16. {
  17. struct platform_device *pdev;
  18. struct resource res[2 + MAX_RESOURCE_DMA];
  19. int i, ret = 0, nres = 0;
  20. pdev = platform_device_alloc(desc->drv_name, desc->id);
  21. if (pdev == NULL)
  22. return -ENOMEM;
  23. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  24. memset(res, 0, sizeof(res));
  25. if (desc->start != -1ul && desc->size > 0) {
  26. res[nres].start = desc->start;
  27. res[nres].end = desc->start + desc->size - 1;
  28. res[nres].flags = IORESOURCE_MEM;
  29. nres++;
  30. }
  31. if (desc->irq != NO_IRQ) {
  32. res[nres].start = desc->irq;
  33. res[nres].end = desc->irq;
  34. res[nres].flags = IORESOURCE_IRQ;
  35. nres++;
  36. }
  37. for (i = 0; i < MAX_RESOURCE_DMA; i++, nres++) {
  38. if (desc->dma[i] == 0)
  39. break;
  40. res[nres].start = desc->dma[i];
  41. res[nres].end = desc->dma[i];
  42. res[nres].flags = IORESOURCE_DMA;
  43. }
  44. ret = platform_device_add_resources(pdev, res, nres);
  45. if (ret) {
  46. platform_device_put(pdev);
  47. return ret;
  48. }
  49. if (data && size) {
  50. ret = platform_device_add_data(pdev, data, size);
  51. if (ret) {
  52. platform_device_put(pdev);
  53. return ret;
  54. }
  55. }
  56. return platform_device_add(pdev);
  57. }
  58. #if IS_ENABLED(CONFIG_USB) || IS_ENABLED(CONFIG_USB_GADGET)
  59. #if IS_ENABLED(CONFIG_USB_MV_UDC) || IS_ENABLED(CONFIG_USB_EHCI_MV)
  60. #if IS_ENABLED(CONFIG_CPU_PXA910) || IS_ENABLED(CONFIG_CPU_PXA168)
  61. /*****************************************************************************
  62. * The registers read/write routines
  63. *****************************************************************************/
  64. static unsigned int u2o_get(void __iomem *base, unsigned int offset)
  65. {
  66. return readl_relaxed(base + offset);
  67. }
  68. static void u2o_set(void __iomem *base, unsigned int offset,
  69. unsigned int value)
  70. {
  71. u32 reg;
  72. reg = readl_relaxed(base + offset);
  73. reg |= value;
  74. writel_relaxed(reg, base + offset);
  75. readl_relaxed(base + offset);
  76. }
  77. static void u2o_clear(void __iomem *base, unsigned int offset,
  78. unsigned int value)
  79. {
  80. u32 reg;
  81. reg = readl_relaxed(base + offset);
  82. reg &= ~value;
  83. writel_relaxed(reg, base + offset);
  84. readl_relaxed(base + offset);
  85. }
  86. static void u2o_write(void __iomem *base, unsigned int offset,
  87. unsigned int value)
  88. {
  89. writel_relaxed(value, base + offset);
  90. readl_relaxed(base + offset);
  91. }
  92. static DEFINE_MUTEX(phy_lock);
  93. static int phy_init_cnt;
  94. static int usb_phy_init_internal(void __iomem *base)
  95. {
  96. int loops;
  97. pr_info("Init usb phy!!!\n");
  98. /* Initialize the USB PHY power */
  99. if (cpu_is_pxa910()) {
  100. u2o_set(base, UTMI_CTRL, (1<<UTMI_CTRL_INPKT_DELAY_SOF_SHIFT)
  101. | (1<<UTMI_CTRL_PU_REF_SHIFT));
  102. }
  103. u2o_set(base, UTMI_CTRL, 1<<UTMI_CTRL_PLL_PWR_UP_SHIFT);
  104. u2o_set(base, UTMI_CTRL, 1<<UTMI_CTRL_PWR_UP_SHIFT);
  105. /* UTMI_PLL settings */
  106. u2o_clear(base, UTMI_PLL, UTMI_PLL_PLLVDD18_MASK
  107. | UTMI_PLL_PLLVDD12_MASK | UTMI_PLL_PLLCALI12_MASK
  108. | UTMI_PLL_FBDIV_MASK | UTMI_PLL_REFDIV_MASK
  109. | UTMI_PLL_ICP_MASK | UTMI_PLL_KVCO_MASK);
  110. u2o_set(base, UTMI_PLL, 0xee<<UTMI_PLL_FBDIV_SHIFT
  111. | 0xb<<UTMI_PLL_REFDIV_SHIFT | 3<<UTMI_PLL_PLLVDD18_SHIFT
  112. | 3<<UTMI_PLL_PLLVDD12_SHIFT | 3<<UTMI_PLL_PLLCALI12_SHIFT
  113. | 1<<UTMI_PLL_ICP_SHIFT | 3<<UTMI_PLL_KVCO_SHIFT);
  114. /* UTMI_TX */
  115. u2o_clear(base, UTMI_TX, UTMI_TX_REG_EXT_FS_RCAL_EN_MASK
  116. | UTMI_TX_TXVDD12_MASK | UTMI_TX_CK60_PHSEL_MASK
  117. | UTMI_TX_IMPCAL_VTH_MASK | UTMI_TX_REG_EXT_FS_RCAL_MASK
  118. | UTMI_TX_AMP_MASK);
  119. u2o_set(base, UTMI_TX, 3<<UTMI_TX_TXVDD12_SHIFT
  120. | 4<<UTMI_TX_CK60_PHSEL_SHIFT | 4<<UTMI_TX_IMPCAL_VTH_SHIFT
  121. | 8<<UTMI_TX_REG_EXT_FS_RCAL_SHIFT | 3<<UTMI_TX_AMP_SHIFT);
  122. /* UTMI_RX */
  123. u2o_clear(base, UTMI_RX, UTMI_RX_SQ_THRESH_MASK
  124. | UTMI_REG_SQ_LENGTH_MASK);
  125. u2o_set(base, UTMI_RX, 7<<UTMI_RX_SQ_THRESH_SHIFT
  126. | 2<<UTMI_REG_SQ_LENGTH_SHIFT);
  127. /* UTMI_IVREF */
  128. if (cpu_is_pxa168())
  129. /* fixing Microsoft Altair board interface with NEC hub issue -
  130. * Set UTMI_IVREF from 0x4a3 to 0x4bf */
  131. u2o_write(base, UTMI_IVREF, 0x4bf);
  132. /* toggle VCOCAL_START bit of UTMI_PLL */
  133. udelay(200);
  134. u2o_set(base, UTMI_PLL, VCOCAL_START);
  135. udelay(40);
  136. u2o_clear(base, UTMI_PLL, VCOCAL_START);
  137. /* toggle REG_RCAL_START bit of UTMI_TX */
  138. udelay(400);
  139. u2o_set(base, UTMI_TX, REG_RCAL_START);
  140. udelay(40);
  141. u2o_clear(base, UTMI_TX, REG_RCAL_START);
  142. udelay(400);
  143. /* Make sure PHY PLL is ready */
  144. loops = 0;
  145. while ((u2o_get(base, UTMI_PLL) & PLL_READY) == 0) {
  146. mdelay(1);
  147. loops++;
  148. if (loops > 100) {
  149. printk(KERN_WARNING "calibrate timeout, UTMI_PLL %x\n",
  150. u2o_get(base, UTMI_PLL));
  151. break;
  152. }
  153. }
  154. if (cpu_is_pxa168()) {
  155. u2o_set(base, UTMI_RESERVE, 1 << 5);
  156. /* Turn on UTMI PHY OTG extension */
  157. u2o_write(base, UTMI_OTG_ADDON, 1);
  158. }
  159. return 0;
  160. }
  161. static int usb_phy_deinit_internal(void __iomem *base)
  162. {
  163. pr_info("Deinit usb phy!!!\n");
  164. if (cpu_is_pxa168())
  165. u2o_clear(base, UTMI_OTG_ADDON, UTMI_OTG_ADDON_OTG_ON);
  166. u2o_clear(base, UTMI_CTRL, UTMI_CTRL_RXBUF_PDWN);
  167. u2o_clear(base, UTMI_CTRL, UTMI_CTRL_TXBUF_PDWN);
  168. u2o_clear(base, UTMI_CTRL, UTMI_CTRL_USB_CLK_EN);
  169. u2o_clear(base, UTMI_CTRL, 1<<UTMI_CTRL_PWR_UP_SHIFT);
  170. u2o_clear(base, UTMI_CTRL, 1<<UTMI_CTRL_PLL_PWR_UP_SHIFT);
  171. return 0;
  172. }
  173. int pxa_usb_phy_init(void __iomem *phy_reg)
  174. {
  175. mutex_lock(&phy_lock);
  176. if (phy_init_cnt++ == 0)
  177. usb_phy_init_internal(phy_reg);
  178. mutex_unlock(&phy_lock);
  179. return 0;
  180. }
  181. void pxa_usb_phy_deinit(void __iomem *phy_reg)
  182. {
  183. WARN_ON(phy_init_cnt == 0);
  184. mutex_lock(&phy_lock);
  185. if (--phy_init_cnt == 0)
  186. usb_phy_deinit_internal(phy_reg);
  187. mutex_unlock(&phy_lock);
  188. }
  189. #endif
  190. #endif
  191. #endif
  192. #if IS_ENABLED(CONFIG_USB_SUPPORT)
  193. static u64 __maybe_unused usb_dma_mask = ~(u32)0;
  194. #if IS_ENABLED(CONFIG_PHY_PXA_USB)
  195. static struct resource pxa168_usb_phy_resources[] = {
  196. [0] = {
  197. .start = PXA168_U2O_PHYBASE,
  198. .end = PXA168_U2O_PHYBASE + USB_PHY_RANGE,
  199. .flags = IORESOURCE_MEM,
  200. },
  201. };
  202. struct platform_device pxa168_device_usb_phy = {
  203. .name = "pxa-usb-phy",
  204. .id = -1,
  205. .resource = pxa168_usb_phy_resources,
  206. .num_resources = ARRAY_SIZE(pxa168_usb_phy_resources),
  207. .dev = {
  208. .dma_mask = &usb_dma_mask,
  209. .coherent_dma_mask = 0xffffffff,
  210. }
  211. };
  212. #endif /* CONFIG_PHY_PXA_USB */
  213. #if IS_ENABLED(CONFIG_USB_MV_UDC)
  214. static struct resource pxa168_u2o_resources[] = {
  215. /* regbase */
  216. [0] = {
  217. .start = PXA168_U2O_REGBASE + U2x_CAPREGS_OFFSET,
  218. .end = PXA168_U2O_REGBASE + USB_REG_RANGE,
  219. .flags = IORESOURCE_MEM,
  220. .name = "capregs",
  221. },
  222. /* phybase */
  223. [1] = {
  224. .start = PXA168_U2O_PHYBASE,
  225. .end = PXA168_U2O_PHYBASE + USB_PHY_RANGE,
  226. .flags = IORESOURCE_MEM,
  227. .name = "phyregs",
  228. },
  229. [2] = {
  230. .start = IRQ_PXA168_USB1,
  231. .end = IRQ_PXA168_USB1,
  232. .flags = IORESOURCE_IRQ,
  233. },
  234. };
  235. struct platform_device pxa168_device_u2o = {
  236. .name = "mv-udc",
  237. .id = -1,
  238. .resource = pxa168_u2o_resources,
  239. .num_resources = ARRAY_SIZE(pxa168_u2o_resources),
  240. .dev = {
  241. .dma_mask = &usb_dma_mask,
  242. .coherent_dma_mask = 0xffffffff,
  243. }
  244. };
  245. #endif /* CONFIG_USB_MV_UDC */
  246. #if IS_ENABLED(CONFIG_USB_EHCI_MV_U2O)
  247. static struct resource pxa168_u2oehci_resources[] = {
  248. [0] = {
  249. .start = PXA168_U2O_REGBASE,
  250. .end = PXA168_U2O_REGBASE + USB_REG_RANGE,
  251. .flags = IORESOURCE_MEM,
  252. },
  253. [1] = {
  254. .start = IRQ_PXA168_USB1,
  255. .end = IRQ_PXA168_USB1,
  256. .flags = IORESOURCE_IRQ,
  257. },
  258. };
  259. struct platform_device pxa168_device_u2oehci = {
  260. .name = "pxa-u2oehci",
  261. .id = -1,
  262. .dev = {
  263. .dma_mask = &usb_dma_mask,
  264. .coherent_dma_mask = 0xffffffff,
  265. },
  266. .num_resources = ARRAY_SIZE(pxa168_u2oehci_resources),
  267. .resource = pxa168_u2oehci_resources,
  268. };
  269. #endif
  270. #if IS_ENABLED(CONFIG_USB_MV_OTG)
  271. static struct resource pxa168_u2ootg_resources[] = {
  272. /* regbase */
  273. [0] = {
  274. .start = PXA168_U2O_REGBASE + U2x_CAPREGS_OFFSET,
  275. .end = PXA168_U2O_REGBASE + USB_REG_RANGE,
  276. .flags = IORESOURCE_MEM,
  277. .name = "capregs",
  278. },
  279. /* phybase */
  280. [1] = {
  281. .start = PXA168_U2O_PHYBASE,
  282. .end = PXA168_U2O_PHYBASE + USB_PHY_RANGE,
  283. .flags = IORESOURCE_MEM,
  284. .name = "phyregs",
  285. },
  286. [2] = {
  287. .start = IRQ_PXA168_USB1,
  288. .end = IRQ_PXA168_USB1,
  289. .flags = IORESOURCE_IRQ,
  290. },
  291. };
  292. struct platform_device pxa168_device_u2ootg = {
  293. .name = "mv-otg",
  294. .id = -1,
  295. .dev = {
  296. .dma_mask = &usb_dma_mask,
  297. .coherent_dma_mask = 0xffffffff,
  298. },
  299. .num_resources = ARRAY_SIZE(pxa168_u2ootg_resources),
  300. .resource = pxa168_u2ootg_resources,
  301. };
  302. #endif /* CONFIG_USB_MV_OTG */
  303. #endif