sta2x11-fixup.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. * arch/x86/pci/sta2x11-fixup.c
  3. * glue code for lib/swiotlb.c and DMA translation between STA2x11
  4. * AMBA memory mapping and the X86 memory mapping
  5. *
  6. * ST Microelectronics ConneXt (STA2X11/STA2X10)
  7. *
  8. * Copyright (c) 2010-2011 Wind River Systems, Inc.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. * See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. */
  24. #include <linux/pci.h>
  25. #include <linux/pci_ids.h>
  26. #include <linux/export.h>
  27. #include <linux/list.h>
  28. #define STA2X11_SWIOTLB_SIZE (4*1024*1024)
  29. extern int swiotlb_late_init_with_default_size(size_t default_size);
  30. /*
  31. * We build a list of bus numbers that are under the ConneXt. The
  32. * main bridge hosts 4 busses, which are the 4 endpoints, in order.
  33. */
  34. #define STA2X11_NR_EP 4 /* 0..3 included */
  35. #define STA2X11_NR_FUNCS 8 /* 0..7 included */
  36. #define STA2X11_AMBA_SIZE (512 << 20)
  37. struct sta2x11_ahb_regs { /* saved during suspend */
  38. u32 base, pexlbase, pexhbase, crw;
  39. };
  40. struct sta2x11_mapping {
  41. u32 amba_base;
  42. int is_suspended;
  43. struct sta2x11_ahb_regs regs[STA2X11_NR_FUNCS];
  44. };
  45. struct sta2x11_instance {
  46. struct list_head list;
  47. int bus0;
  48. struct sta2x11_mapping map[STA2X11_NR_EP];
  49. };
  50. static LIST_HEAD(sta2x11_instance_list);
  51. /* At probe time, record new instances of this bridge (likely one only) */
  52. static void sta2x11_new_instance(struct pci_dev *pdev)
  53. {
  54. struct sta2x11_instance *instance;
  55. instance = kzalloc(sizeof(*instance), GFP_ATOMIC);
  56. if (!instance)
  57. return;
  58. /* This has a subordinate bridge, with 4 more-subordinate ones */
  59. instance->bus0 = pdev->subordinate->number + 1;
  60. if (list_empty(&sta2x11_instance_list)) {
  61. int size = STA2X11_SWIOTLB_SIZE;
  62. /* First instance: register your own swiotlb area */
  63. dev_info(&pdev->dev, "Using SWIOTLB (size %i)\n", size);
  64. if (swiotlb_late_init_with_default_size(size))
  65. dev_emerg(&pdev->dev, "init swiotlb failed\n");
  66. }
  67. list_add(&instance->list, &sta2x11_instance_list);
  68. }
  69. DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, 0xcc17, sta2x11_new_instance);
  70. /*
  71. * Utility functions used in this file from below
  72. */
  73. static struct sta2x11_instance *sta2x11_pdev_to_instance(struct pci_dev *pdev)
  74. {
  75. struct sta2x11_instance *instance;
  76. int ep;
  77. list_for_each_entry(instance, &sta2x11_instance_list, list) {
  78. ep = pdev->bus->number - instance->bus0;
  79. if (ep >= 0 && ep < STA2X11_NR_EP)
  80. return instance;
  81. }
  82. return NULL;
  83. }
  84. static int sta2x11_pdev_to_ep(struct pci_dev *pdev)
  85. {
  86. struct sta2x11_instance *instance;
  87. instance = sta2x11_pdev_to_instance(pdev);
  88. if (!instance)
  89. return -1;
  90. return pdev->bus->number - instance->bus0;
  91. }
  92. static struct sta2x11_mapping *sta2x11_pdev_to_mapping(struct pci_dev *pdev)
  93. {
  94. struct sta2x11_instance *instance;
  95. int ep;
  96. instance = sta2x11_pdev_to_instance(pdev);
  97. if (!instance)
  98. return NULL;
  99. ep = sta2x11_pdev_to_ep(pdev);
  100. return instance->map + ep;
  101. }
  102. /* This is exported, as some devices need to access the MFD registers */
  103. struct sta2x11_instance *sta2x11_get_instance(struct pci_dev *pdev)
  104. {
  105. return sta2x11_pdev_to_instance(pdev);
  106. }
  107. EXPORT_SYMBOL(sta2x11_get_instance);
  108. /**
  109. * p2a - Translate physical address to STA2x11 AMBA address,
  110. * used for DMA transfers to STA2x11
  111. * @p: Physical address
  112. * @pdev: PCI device (must be hosted within the connext)
  113. */
  114. static dma_addr_t p2a(dma_addr_t p, struct pci_dev *pdev)
  115. {
  116. struct sta2x11_mapping *map;
  117. dma_addr_t a;
  118. map = sta2x11_pdev_to_mapping(pdev);
  119. a = p + map->amba_base;
  120. return a;
  121. }
  122. /**
  123. * a2p - Translate STA2x11 AMBA address to physical address
  124. * used for DMA transfers from STA2x11
  125. * @a: STA2x11 AMBA address
  126. * @pdev: PCI device (must be hosted within the connext)
  127. */
  128. static dma_addr_t a2p(dma_addr_t a, struct pci_dev *pdev)
  129. {
  130. struct sta2x11_mapping *map;
  131. dma_addr_t p;
  132. map = sta2x11_pdev_to_mapping(pdev);
  133. p = a - map->amba_base;
  134. return p;
  135. }
  136. /**
  137. * sta2x11_swiotlb_alloc_coherent - Allocate swiotlb bounce buffers
  138. * returns virtual address. This is the only "special" function here.
  139. * @dev: PCI device
  140. * @size: Size of the buffer
  141. * @dma_handle: DMA address
  142. * @flags: memory flags
  143. */
  144. static void *sta2x11_swiotlb_alloc_coherent(struct device *dev,
  145. size_t size,
  146. dma_addr_t *dma_handle,
  147. gfp_t flags,
  148. struct dma_attrs *attrs)
  149. {
  150. void *vaddr;
  151. vaddr = x86_swiotlb_alloc_coherent(dev, size, dma_handle, flags, attrs);
  152. *dma_handle = p2a(*dma_handle, to_pci_dev(dev));
  153. return vaddr;
  154. }
  155. /* We have our own dma_ops: the same as swiotlb but from alloc (above) */
  156. static struct dma_map_ops sta2x11_dma_ops = {
  157. .alloc = sta2x11_swiotlb_alloc_coherent,
  158. .free = x86_swiotlb_free_coherent,
  159. .map_page = swiotlb_map_page,
  160. .unmap_page = swiotlb_unmap_page,
  161. .map_sg = swiotlb_map_sg_attrs,
  162. .unmap_sg = swiotlb_unmap_sg_attrs,
  163. .sync_single_for_cpu = swiotlb_sync_single_for_cpu,
  164. .sync_single_for_device = swiotlb_sync_single_for_device,
  165. .sync_sg_for_cpu = swiotlb_sync_sg_for_cpu,
  166. .sync_sg_for_device = swiotlb_sync_sg_for_device,
  167. .mapping_error = swiotlb_dma_mapping_error,
  168. .dma_supported = NULL, /* FIXME: we should use this instead! */
  169. };
  170. /* At setup time, we use our own ops if the device is a ConneXt one */
  171. static void sta2x11_setup_pdev(struct pci_dev *pdev)
  172. {
  173. struct sta2x11_instance *instance = sta2x11_pdev_to_instance(pdev);
  174. if (!instance) /* either a sta2x11 bridge or another ST device */
  175. return;
  176. pci_set_consistent_dma_mask(pdev, STA2X11_AMBA_SIZE - 1);
  177. pci_set_dma_mask(pdev, STA2X11_AMBA_SIZE - 1);
  178. pdev->dev.archdata.dma_ops = &sta2x11_dma_ops;
  179. /* We must enable all devices as master, for audio DMA to work */
  180. pci_set_master(pdev);
  181. }
  182. DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, sta2x11_setup_pdev);
  183. /*
  184. * The following three functions are exported (used in swiotlb: FIXME)
  185. */
  186. /**
  187. * dma_capable - Check if device can manage DMA transfers (FIXME: kill it)
  188. * @dev: device for a PCI device
  189. * @addr: DMA address
  190. * @size: DMA size
  191. */
  192. bool dma_capable(struct device *dev, dma_addr_t addr, size_t size)
  193. {
  194. struct sta2x11_mapping *map;
  195. if (dev->archdata.dma_ops != &sta2x11_dma_ops) {
  196. if (!dev->dma_mask)
  197. return false;
  198. return addr + size - 1 <= *dev->dma_mask;
  199. }
  200. map = sta2x11_pdev_to_mapping(to_pci_dev(dev));
  201. if (!map || (addr < map->amba_base))
  202. return false;
  203. if (addr + size >= map->amba_base + STA2X11_AMBA_SIZE) {
  204. return false;
  205. }
  206. return true;
  207. }
  208. /**
  209. * phys_to_dma - Return the DMA AMBA address used for this STA2x11 device
  210. * @dev: device for a PCI device
  211. * @paddr: Physical address
  212. */
  213. dma_addr_t phys_to_dma(struct device *dev, phys_addr_t paddr)
  214. {
  215. if (dev->archdata.dma_ops != &sta2x11_dma_ops)
  216. return paddr;
  217. return p2a(paddr, to_pci_dev(dev));
  218. }
  219. /**
  220. * dma_to_phys - Return the physical address used for this STA2x11 DMA address
  221. * @dev: device for a PCI device
  222. * @daddr: STA2x11 AMBA DMA address
  223. */
  224. phys_addr_t dma_to_phys(struct device *dev, dma_addr_t daddr)
  225. {
  226. if (dev->archdata.dma_ops != &sta2x11_dma_ops)
  227. return daddr;
  228. return a2p(daddr, to_pci_dev(dev));
  229. }
  230. /*
  231. * At boot we must set up the mappings for the pcie-to-amba bridge.
  232. * It involves device access, and the same happens at suspend/resume time
  233. */
  234. #define AHB_MAPB 0xCA4
  235. #define AHB_CRW(i) (AHB_MAPB + 0 + (i) * 0x10)
  236. #define AHB_CRW_SZMASK 0xfffffc00UL
  237. #define AHB_CRW_ENABLE (1 << 0)
  238. #define AHB_CRW_WTYPE_MEM (2 << 1)
  239. #define AHB_CRW_ROE (1UL << 3) /* Relax Order Ena */
  240. #define AHB_CRW_NSE (1UL << 4) /* No Snoop Enable */
  241. #define AHB_BASE(i) (AHB_MAPB + 4 + (i) * 0x10)
  242. #define AHB_PEXLBASE(i) (AHB_MAPB + 8 + (i) * 0x10)
  243. #define AHB_PEXHBASE(i) (AHB_MAPB + 12 + (i) * 0x10)
  244. /* At probe time, enable mapping for each endpoint, using the pdev */
  245. static void sta2x11_map_ep(struct pci_dev *pdev)
  246. {
  247. struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
  248. int i;
  249. if (!map)
  250. return;
  251. pci_read_config_dword(pdev, AHB_BASE(0), &map->amba_base);
  252. /* Configure AHB mapping */
  253. pci_write_config_dword(pdev, AHB_PEXLBASE(0), 0);
  254. pci_write_config_dword(pdev, AHB_PEXHBASE(0), 0);
  255. pci_write_config_dword(pdev, AHB_CRW(0), STA2X11_AMBA_SIZE |
  256. AHB_CRW_WTYPE_MEM | AHB_CRW_ENABLE);
  257. /* Disable all the other windows */
  258. for (i = 1; i < STA2X11_NR_FUNCS; i++)
  259. pci_write_config_dword(pdev, AHB_CRW(i), 0);
  260. dev_info(&pdev->dev,
  261. "sta2x11: Map EP %i: AMBA address %#8x-%#8x\n",
  262. sta2x11_pdev_to_ep(pdev), map->amba_base,
  263. map->amba_base + STA2X11_AMBA_SIZE - 1);
  264. }
  265. DECLARE_PCI_FIXUP_ENABLE(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, sta2x11_map_ep);
  266. #ifdef CONFIG_PM /* Some register values must be saved and restored */
  267. static void suspend_mapping(struct pci_dev *pdev)
  268. {
  269. struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
  270. int i;
  271. if (!map)
  272. return;
  273. if (map->is_suspended)
  274. return;
  275. map->is_suspended = 1;
  276. /* Save all window configs */
  277. for (i = 0; i < STA2X11_NR_FUNCS; i++) {
  278. struct sta2x11_ahb_regs *regs = map->regs + i;
  279. pci_read_config_dword(pdev, AHB_BASE(i), &regs->base);
  280. pci_read_config_dword(pdev, AHB_PEXLBASE(i), &regs->pexlbase);
  281. pci_read_config_dword(pdev, AHB_PEXHBASE(i), &regs->pexhbase);
  282. pci_read_config_dword(pdev, AHB_CRW(i), &regs->crw);
  283. }
  284. }
  285. DECLARE_PCI_FIXUP_SUSPEND(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, suspend_mapping);
  286. static void resume_mapping(struct pci_dev *pdev)
  287. {
  288. struct sta2x11_mapping *map = sta2x11_pdev_to_mapping(pdev);
  289. int i;
  290. if (!map)
  291. return;
  292. if (!map->is_suspended)
  293. goto out;
  294. map->is_suspended = 0;
  295. /* Restore all window configs */
  296. for (i = 0; i < STA2X11_NR_FUNCS; i++) {
  297. struct sta2x11_ahb_regs *regs = map->regs + i;
  298. pci_write_config_dword(pdev, AHB_BASE(i), regs->base);
  299. pci_write_config_dword(pdev, AHB_PEXLBASE(i), regs->pexlbase);
  300. pci_write_config_dword(pdev, AHB_PEXHBASE(i), regs->pexhbase);
  301. pci_write_config_dword(pdev, AHB_CRW(i), regs->crw);
  302. }
  303. out:
  304. pci_set_master(pdev); /* Like at boot, enable master on all devices */
  305. }
  306. DECLARE_PCI_FIXUP_RESUME(PCI_VENDOR_ID_STMICRO, PCI_ANY_ID, resume_mapping);
  307. #endif /* CONFIG_PM */