spear13xx_pcie_gadget.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * drivers/misc/spear13xx_pcie_gadget.c
  3. *
  4. * Copyright (C) 2010 ST Microelectronics
  5. * Pratyush Anand<pratyush.anand@gmail.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/device.h>
  12. #include <linux/clk.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/irq.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pci_regs.h>
  22. #include <linux/configfs.h>
  23. #include <mach/pcie.h>
  24. #include <mach/misc_regs.h>
  25. #define IN0_MEM_SIZE (200 * 1024 * 1024 - 1)
  26. /* In current implementation address translation is done using IN0 only.
  27. * So IN1 start address and IN0 end address has been kept same
  28. */
  29. #define IN1_MEM_SIZE (0 * 1024 * 1024 - 1)
  30. #define IN_IO_SIZE (20 * 1024 * 1024 - 1)
  31. #define IN_CFG0_SIZE (12 * 1024 * 1024 - 1)
  32. #define IN_CFG1_SIZE (12 * 1024 * 1024 - 1)
  33. #define IN_MSG_SIZE (12 * 1024 * 1024 - 1)
  34. /* Keep default BAR size as 4K*/
  35. /* AORAM would be mapped by default*/
  36. #define INBOUND_ADDR_MASK (SPEAR13XX_SYSRAM1_SIZE - 1)
  37. #define INT_TYPE_NO_INT 0
  38. #define INT_TYPE_INTX 1
  39. #define INT_TYPE_MSI 2
  40. struct spear_pcie_gadget_config {
  41. void __iomem *base;
  42. void __iomem *va_app_base;
  43. void __iomem *va_dbi_base;
  44. char int_type[10];
  45. ulong requested_msi;
  46. ulong configured_msi;
  47. ulong bar0_size;
  48. ulong bar0_rw_offset;
  49. void __iomem *va_bar0_address;
  50. };
  51. struct pcie_gadget_target {
  52. struct configfs_subsystem subsys;
  53. struct spear_pcie_gadget_config config;
  54. };
  55. struct pcie_gadget_target_attr {
  56. struct configfs_attribute attr;
  57. ssize_t (*show)(struct spear_pcie_gadget_config *config,
  58. char *buf);
  59. ssize_t (*store)(struct spear_pcie_gadget_config *config,
  60. const char *buf,
  61. size_t count);
  62. };
  63. static void enable_dbi_access(struct pcie_app_reg __iomem *app_reg)
  64. {
  65. /* Enable DBI access */
  66. writel(readl(&app_reg->slv_armisc) | (1 << AXI_OP_DBI_ACCESS_ID),
  67. &app_reg->slv_armisc);
  68. writel(readl(&app_reg->slv_awmisc) | (1 << AXI_OP_DBI_ACCESS_ID),
  69. &app_reg->slv_awmisc);
  70. }
  71. static void disable_dbi_access(struct pcie_app_reg __iomem *app_reg)
  72. {
  73. /* disable DBI access */
  74. writel(readl(&app_reg->slv_armisc) & ~(1 << AXI_OP_DBI_ACCESS_ID),
  75. &app_reg->slv_armisc);
  76. writel(readl(&app_reg->slv_awmisc) & ~(1 << AXI_OP_DBI_ACCESS_ID),
  77. &app_reg->slv_awmisc);
  78. }
  79. static void spear_dbi_read_reg(struct spear_pcie_gadget_config *config,
  80. int where, int size, u32 *val)
  81. {
  82. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  83. ulong va_address;
  84. /* Enable DBI access */
  85. enable_dbi_access(app_reg);
  86. va_address = (ulong)config->va_dbi_base + (where & ~0x3);
  87. *val = readl(va_address);
  88. if (size == 1)
  89. *val = (*val >> (8 * (where & 3))) & 0xff;
  90. else if (size == 2)
  91. *val = (*val >> (8 * (where & 3))) & 0xffff;
  92. /* Disable DBI access */
  93. disable_dbi_access(app_reg);
  94. }
  95. static void spear_dbi_write_reg(struct spear_pcie_gadget_config *config,
  96. int where, int size, u32 val)
  97. {
  98. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  99. ulong va_address;
  100. /* Enable DBI access */
  101. enable_dbi_access(app_reg);
  102. va_address = (ulong)config->va_dbi_base + (where & ~0x3);
  103. if (size == 4)
  104. writel(val, va_address);
  105. else if (size == 2)
  106. writew(val, va_address + (where & 2));
  107. else if (size == 1)
  108. writeb(val, va_address + (where & 3));
  109. /* Disable DBI access */
  110. disable_dbi_access(app_reg);
  111. }
  112. #define PCI_FIND_CAP_TTL 48
  113. static int pci_find_own_next_cap_ttl(struct spear_pcie_gadget_config *config,
  114. u32 pos, int cap, int *ttl)
  115. {
  116. u32 id;
  117. while ((*ttl)--) {
  118. spear_dbi_read_reg(config, pos, 1, &pos);
  119. if (pos < 0x40)
  120. break;
  121. pos &= ~3;
  122. spear_dbi_read_reg(config, pos + PCI_CAP_LIST_ID, 1, &id);
  123. if (id == 0xff)
  124. break;
  125. if (id == cap)
  126. return pos;
  127. pos += PCI_CAP_LIST_NEXT;
  128. }
  129. return 0;
  130. }
  131. static int pci_find_own_next_cap(struct spear_pcie_gadget_config *config,
  132. u32 pos, int cap)
  133. {
  134. int ttl = PCI_FIND_CAP_TTL;
  135. return pci_find_own_next_cap_ttl(config, pos, cap, &ttl);
  136. }
  137. static int pci_find_own_cap_start(struct spear_pcie_gadget_config *config,
  138. u8 hdr_type)
  139. {
  140. u32 status;
  141. spear_dbi_read_reg(config, PCI_STATUS, 2, &status);
  142. if (!(status & PCI_STATUS_CAP_LIST))
  143. return 0;
  144. switch (hdr_type) {
  145. case PCI_HEADER_TYPE_NORMAL:
  146. case PCI_HEADER_TYPE_BRIDGE:
  147. return PCI_CAPABILITY_LIST;
  148. case PCI_HEADER_TYPE_CARDBUS:
  149. return PCI_CB_CAPABILITY_LIST;
  150. default:
  151. return 0;
  152. }
  153. return 0;
  154. }
  155. /*
  156. * Tell if a device supports a given PCI capability.
  157. * Returns the address of the requested capability structure within the
  158. * device's PCI configuration space or 0 in case the device does not
  159. * support it. Possible values for @cap:
  160. *
  161. * %PCI_CAP_ID_PM Power Management
  162. * %PCI_CAP_ID_AGP Accelerated Graphics Port
  163. * %PCI_CAP_ID_VPD Vital Product Data
  164. * %PCI_CAP_ID_SLOTID Slot Identification
  165. * %PCI_CAP_ID_MSI Message Signalled Interrupts
  166. * %PCI_CAP_ID_CHSWP CompactPCI HotSwap
  167. * %PCI_CAP_ID_PCIX PCI-X
  168. * %PCI_CAP_ID_EXP PCI Express
  169. */
  170. static int pci_find_own_capability(struct spear_pcie_gadget_config *config,
  171. int cap)
  172. {
  173. u32 pos;
  174. u32 hdr_type;
  175. spear_dbi_read_reg(config, PCI_HEADER_TYPE, 1, &hdr_type);
  176. pos = pci_find_own_cap_start(config, hdr_type);
  177. if (pos)
  178. pos = pci_find_own_next_cap(config, pos, cap);
  179. return pos;
  180. }
  181. static irqreturn_t spear_pcie_gadget_irq(int irq, void *dev_id)
  182. {
  183. return 0;
  184. }
  185. /*
  186. * configfs interfaces show/store functions
  187. */
  188. static struct pcie_gadget_target *to_target(struct config_item *item)
  189. {
  190. return item ?
  191. container_of(to_configfs_subsystem(to_config_group(item)),
  192. struct pcie_gadget_target, subsys) : NULL;
  193. }
  194. static ssize_t pcie_gadget_link_show(struct config_item *item, char *buf)
  195. {
  196. struct pcie_app_reg __iomem *app_reg = to_target(item)->va_app_base;
  197. if (readl(&app_reg->app_status_1) & ((u32)1 << XMLH_LINK_UP_ID))
  198. return sprintf(buf, "UP");
  199. else
  200. return sprintf(buf, "DOWN");
  201. }
  202. static ssize_t pcie_gadget_link_store(struct config_item *item,
  203. const char *buf, size_t count)
  204. {
  205. struct pcie_app_reg __iomem *app_reg = to_target(item)->va_app_base;
  206. if (sysfs_streq(buf, "UP"))
  207. writel(readl(&app_reg->app_ctrl_0) | (1 << APP_LTSSM_ENABLE_ID),
  208. &app_reg->app_ctrl_0);
  209. else if (sysfs_streq(buf, "DOWN"))
  210. writel(readl(&app_reg->app_ctrl_0)
  211. & ~(1 << APP_LTSSM_ENABLE_ID),
  212. &app_reg->app_ctrl_0);
  213. else
  214. return -EINVAL;
  215. return count;
  216. }
  217. static ssize_t pcie_gadget_int_type_show(struct config_item *item, char *buf)
  218. {
  219. return sprintf(buf, "%s", to_target(item)->int_type);
  220. }
  221. static ssize_t pcie_gadget_int_type_store(struct config_item *item,
  222. const char *buf, size_t count)
  223. {
  224. struct spear_pcie_gadget_config *config = to_target(item)
  225. u32 cap, vec, flags;
  226. ulong vector;
  227. if (sysfs_streq(buf, "INTA"))
  228. spear_dbi_write_reg(config, PCI_INTERRUPT_LINE, 1, 1);
  229. else if (sysfs_streq(buf, "MSI")) {
  230. vector = config->requested_msi;
  231. vec = 0;
  232. while (vector > 1) {
  233. vector /= 2;
  234. vec++;
  235. }
  236. spear_dbi_write_reg(config, PCI_INTERRUPT_LINE, 1, 0);
  237. cap = pci_find_own_capability(config, PCI_CAP_ID_MSI);
  238. spear_dbi_read_reg(config, cap + PCI_MSI_FLAGS, 1, &flags);
  239. flags &= ~PCI_MSI_FLAGS_QMASK;
  240. flags |= vec << 1;
  241. spear_dbi_write_reg(config, cap + PCI_MSI_FLAGS, 1, flags);
  242. } else
  243. return -EINVAL;
  244. strcpy(config->int_type, buf);
  245. return count;
  246. }
  247. static ssize_t pcie_gadget_no_of_msi_show(struct config_item *item, char *buf)
  248. {
  249. struct spear_pcie_gadget_config *config = to_target(item)
  250. struct pcie_app_reg __iomem *app_reg = to_target(item)->va_app_base;
  251. u32 cap, vec, flags;
  252. ulong vector;
  253. if ((readl(&app_reg->msg_status) & (1 << CFG_MSI_EN_ID))
  254. != (1 << CFG_MSI_EN_ID))
  255. vector = 0;
  256. else {
  257. cap = pci_find_own_capability(config, PCI_CAP_ID_MSI);
  258. spear_dbi_read_reg(config, cap + PCI_MSI_FLAGS, 1, &flags);
  259. flags &= ~PCI_MSI_FLAGS_QSIZE;
  260. vec = flags >> 4;
  261. vector = 1;
  262. while (vec--)
  263. vector *= 2;
  264. }
  265. config->configured_msi = vector;
  266. return sprintf(buf, "%lu", vector);
  267. }
  268. static ssize_t pcie_gadget_no_of_msi_store(struct config_item *item,
  269. const char *buf, size_t count)
  270. {
  271. int ret;
  272. ret = kstrtoul(buf, 0, &to_target(item)->requested_msi);
  273. if (ret)
  274. return ret;
  275. if (config->requested_msi > 32)
  276. config->requested_msi = 32;
  277. return count;
  278. }
  279. static ssize_t pcie_gadget_inta_store(struct config_item *item,
  280. const char *buf, size_t count)
  281. {
  282. struct pcie_app_reg __iomem *app_reg = to_target(item)->va_app_base;
  283. ulong en;
  284. int ret;
  285. ret = kstrtoul(buf, 0, &en);
  286. if (ret)
  287. return ret;
  288. if (en)
  289. writel(readl(&app_reg->app_ctrl_0) | (1 << SYS_INT_ID),
  290. &app_reg->app_ctrl_0);
  291. else
  292. writel(readl(&app_reg->app_ctrl_0) & ~(1 << SYS_INT_ID),
  293. &app_reg->app_ctrl_0);
  294. return count;
  295. }
  296. static ssize_t pcie_gadget_send_msi_store(struct config_item *item,
  297. const char *buf, size_t count)
  298. {
  299. struct spear_pcie_gadget_config *config = to_target(item)
  300. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  301. ulong vector;
  302. u32 ven_msi;
  303. int ret;
  304. ret = kstrtoul(buf, 0, &vector);
  305. if (ret)
  306. return ret;
  307. if (!config->configured_msi)
  308. return -EINVAL;
  309. if (vector >= config->configured_msi)
  310. return -EINVAL;
  311. ven_msi = readl(&app_reg->ven_msi_1);
  312. ven_msi &= ~VEN_MSI_FUN_NUM_MASK;
  313. ven_msi |= 0 << VEN_MSI_FUN_NUM_ID;
  314. ven_msi &= ~VEN_MSI_TC_MASK;
  315. ven_msi |= 0 << VEN_MSI_TC_ID;
  316. ven_msi &= ~VEN_MSI_VECTOR_MASK;
  317. ven_msi |= vector << VEN_MSI_VECTOR_ID;
  318. /* generating interrupt for msi vector */
  319. ven_msi |= VEN_MSI_REQ_EN;
  320. writel(ven_msi, &app_reg->ven_msi_1);
  321. udelay(1);
  322. ven_msi &= ~VEN_MSI_REQ_EN;
  323. writel(ven_msi, &app_reg->ven_msi_1);
  324. return count;
  325. }
  326. static ssize_t pcie_gadget_vendor_id_show(struct config_item *item, char *buf)
  327. {
  328. u32 id;
  329. spear_dbi_read_reg(to_target(item), PCI_VENDOR_ID, 2, &id);
  330. return sprintf(buf, "%x", id);
  331. }
  332. static ssize_t pcie_gadget_vendor_id_store(struct config_item *item,
  333. const char *buf, size_t count)
  334. {
  335. ulong id;
  336. int ret;
  337. ret = kstrtoul(buf, 0, &id);
  338. if (ret)
  339. return ret;
  340. spear_dbi_write_reg(to_target(item), PCI_VENDOR_ID, 2, id);
  341. return count;
  342. }
  343. static ssize_t pcie_gadget_device_id_show(struct config_item *item, char *buf)
  344. {
  345. u32 id;
  346. spear_dbi_read_reg(to_target(item), PCI_DEVICE_ID, 2, &id);
  347. return sprintf(buf, "%x", id);
  348. }
  349. static ssize_t pcie_gadget_device_id_store(struct config_item *item,
  350. const char *buf, size_t count)
  351. {
  352. ulong id;
  353. int ret;
  354. ret = kstrtoul(buf, 0, &id);
  355. if (ret)
  356. return ret;
  357. spear_dbi_write_reg(to_target(item), PCI_DEVICE_ID, 2, id);
  358. return count;
  359. }
  360. static ssize_t pcie_gadget_bar0_size_show(struct config_item *item, char *buf)
  361. {
  362. return sprintf(buf, "%lx", to_target(item)->bar0_size);
  363. }
  364. static ssize_t pcie_gadget_bar0_size_store(struct config_item *item,
  365. const char *buf, size_t count)
  366. {
  367. struct spear_pcie_gadget_config *config = to_target(item)
  368. ulong size;
  369. u32 pos, pos1;
  370. u32 no_of_bit = 0;
  371. int ret;
  372. ret = kstrtoul(buf, 0, &size);
  373. if (ret)
  374. return ret;
  375. /* min bar size is 256 */
  376. if (size <= 0x100)
  377. size = 0x100;
  378. /* max bar size is 1MB*/
  379. else if (size >= 0x100000)
  380. size = 0x100000;
  381. else {
  382. pos = 0;
  383. pos1 = 0;
  384. while (pos < 21) {
  385. pos = find_next_bit((ulong *)&size, 21, pos);
  386. if (pos != 21)
  387. pos1 = pos + 1;
  388. pos++;
  389. no_of_bit++;
  390. }
  391. if (no_of_bit == 2)
  392. pos1--;
  393. size = 1 << pos1;
  394. }
  395. config->bar0_size = size;
  396. spear_dbi_write_reg(config, PCIE_BAR0_MASK_REG, 4, size - 1);
  397. return count;
  398. }
  399. static ssize_t pcie_gadget_bar0_address_show(struct config_item *item,
  400. char *buf)
  401. {
  402. struct pcie_app_reg __iomem *app_reg = to_target(item)->va_app_base;
  403. u32 address = readl(&app_reg->pim0_mem_addr_start);
  404. return sprintf(buf, "%x", address);
  405. }
  406. static ssize_t pcie_gadget_bar0_address_store(struct config_item *item,
  407. const char *buf, size_t count)
  408. {
  409. struct spear_pcie_gadget_config *config = to_target(item)
  410. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  411. ulong address;
  412. int ret;
  413. ret = kstrtoul(buf, 0, &address);
  414. if (ret)
  415. return ret;
  416. address &= ~(config->bar0_size - 1);
  417. if (config->va_bar0_address)
  418. iounmap(config->va_bar0_address);
  419. config->va_bar0_address = ioremap(address, config->bar0_size);
  420. if (!config->va_bar0_address)
  421. return -ENOMEM;
  422. writel(address, &app_reg->pim0_mem_addr_start);
  423. return count;
  424. }
  425. static ssize_t pcie_gadget_bar0_rw_offset_show(struct config_item *item,
  426. char *buf)
  427. {
  428. return sprintf(buf, "%lx", to_target(item)->bar0_rw_offset);
  429. }
  430. static ssize_t pcie_gadget_bar0_rw_offset_store(struct config_item *item,
  431. const char *buf, size_t count)
  432. {
  433. ulong offset;
  434. int ret;
  435. ret = kstrtoul(buf, 0, &offset);
  436. if (ret)
  437. return ret;
  438. if (offset % 4)
  439. return -EINVAL;
  440. to_target(item)->bar0_rw_offset = offset;
  441. return count;
  442. }
  443. static ssize_t pcie_gadget_bar0_data_show(struct config_item *item, char *buf)
  444. {
  445. struct spear_pcie_gadget_config *config = to_target(item)
  446. ulong data;
  447. if (!config->va_bar0_address)
  448. return -ENOMEM;
  449. data = readl((ulong)config->va_bar0_address + config->bar0_rw_offset);
  450. return sprintf(buf, "%lx", data);
  451. }
  452. static ssize_t pcie_gadget_bar0_data_store(struct config_item *item,
  453. const char *buf, size_t count)
  454. {
  455. struct spear_pcie_gadget_config *config = to_target(item)
  456. ulong data;
  457. int ret;
  458. ret = kstrtoul(buf, 0, &data);
  459. if (ret)
  460. return ret;
  461. if (!config->va_bar0_address)
  462. return -ENOMEM;
  463. writel(data, (ulong)config->va_bar0_address + config->bar0_rw_offset);
  464. return count;
  465. }
  466. CONFIGFS_ATTR(pcie_gadget_, link);
  467. CONFIGFS_ATTR(pcie_gadget_, int_type);
  468. CONFIGFS_ATTR(pcie_gadget_, no_of_msi);
  469. CONFIGFS_ATTR_WO(pcie_gadget_, inta);
  470. CONFIGFS_ATTR_WO(pcie_gadget_, send_msi);
  471. CONFIGFS_ATTR(pcie_gadget_, vendor_id);
  472. CONFIGFS_ATTR(pcie_gadget_, device_id);
  473. CONFIGFS_ATTR(pcie_gadget_, bar0_size);
  474. CONFIGFS_ATTR(pcie_gadget_, bar0_address);
  475. CONFIGFS_ATTR(pcie_gadget_, bar0_rw_offset);
  476. CONFIGFS_ATTR(pcie_gadget_, bar0_data);
  477. static struct configfs_attribute *pcie_gadget_target_attrs[] = {
  478. &pcie_gadget_attr_link,
  479. &pcie_gadget_attr_int_type,
  480. &pcie_gadget_attr_no_of_msi,
  481. &pcie_gadget_attr_inta,
  482. &pcie_gadget_attr_send_msi,
  483. &pcie_gadget_attr_vendor_id,
  484. &pcie_gadget_attr_device_id,
  485. &pcie_gadget_attr_bar0_size,
  486. &pcie_gadget_attr_bar0_address,
  487. &pcie_gadget_attr_bar0_rw_offset,
  488. &pcie_gadget_attr_bar0_data,
  489. NULL,
  490. };
  491. static struct config_item_type pcie_gadget_target_type = {
  492. .ct_attrs = pcie_gadget_target_attrs,
  493. .ct_owner = THIS_MODULE,
  494. };
  495. static void spear13xx_pcie_device_init(struct spear_pcie_gadget_config *config)
  496. {
  497. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  498. /*setup registers for outbound translation */
  499. writel(config->base, &app_reg->in0_mem_addr_start);
  500. writel(app_reg->in0_mem_addr_start + IN0_MEM_SIZE,
  501. &app_reg->in0_mem_addr_limit);
  502. writel(app_reg->in0_mem_addr_limit + 1, &app_reg->in1_mem_addr_start);
  503. writel(app_reg->in1_mem_addr_start + IN1_MEM_SIZE,
  504. &app_reg->in1_mem_addr_limit);
  505. writel(app_reg->in1_mem_addr_limit + 1, &app_reg->in_io_addr_start);
  506. writel(app_reg->in_io_addr_start + IN_IO_SIZE,
  507. &app_reg->in_io_addr_limit);
  508. writel(app_reg->in_io_addr_limit + 1, &app_reg->in_cfg0_addr_start);
  509. writel(app_reg->in_cfg0_addr_start + IN_CFG0_SIZE,
  510. &app_reg->in_cfg0_addr_limit);
  511. writel(app_reg->in_cfg0_addr_limit + 1, &app_reg->in_cfg1_addr_start);
  512. writel(app_reg->in_cfg1_addr_start + IN_CFG1_SIZE,
  513. &app_reg->in_cfg1_addr_limit);
  514. writel(app_reg->in_cfg1_addr_limit + 1, &app_reg->in_msg_addr_start);
  515. writel(app_reg->in_msg_addr_start + IN_MSG_SIZE,
  516. &app_reg->in_msg_addr_limit);
  517. writel(app_reg->in0_mem_addr_start, &app_reg->pom0_mem_addr_start);
  518. writel(app_reg->in1_mem_addr_start, &app_reg->pom1_mem_addr_start);
  519. writel(app_reg->in_io_addr_start, &app_reg->pom_io_addr_start);
  520. /*setup registers for inbound translation */
  521. /* Keep AORAM mapped at BAR0 as default */
  522. config->bar0_size = INBOUND_ADDR_MASK + 1;
  523. spear_dbi_write_reg(config, PCIE_BAR0_MASK_REG, 4, INBOUND_ADDR_MASK);
  524. spear_dbi_write_reg(config, PCI_BASE_ADDRESS_0, 4, 0xC);
  525. config->va_bar0_address = ioremap(SPEAR13XX_SYSRAM1_BASE,
  526. config->bar0_size);
  527. writel(SPEAR13XX_SYSRAM1_BASE, &app_reg->pim0_mem_addr_start);
  528. writel(0, &app_reg->pim1_mem_addr_start);
  529. writel(INBOUND_ADDR_MASK + 1, &app_reg->mem0_addr_offset_limit);
  530. writel(0x0, &app_reg->pim_io_addr_start);
  531. writel(0x0, &app_reg->pim_io_addr_start);
  532. writel(0x0, &app_reg->pim_rom_addr_start);
  533. writel(DEVICE_TYPE_EP | (1 << MISCTRL_EN_ID)
  534. | ((u32)1 << REG_TRANSLATION_ENABLE),
  535. &app_reg->app_ctrl_0);
  536. /* disable all rx interrupts */
  537. writel(0, &app_reg->int_mask);
  538. /* Select INTA as default*/
  539. spear_dbi_write_reg(config, PCI_INTERRUPT_LINE, 1, 1);
  540. }
  541. static int spear_pcie_gadget_probe(struct platform_device *pdev)
  542. {
  543. struct resource *res0, *res1;
  544. unsigned int status = 0;
  545. int irq;
  546. struct clk *clk;
  547. static struct pcie_gadget_target *target;
  548. struct spear_pcie_gadget_config *config;
  549. struct config_item *cg_item;
  550. struct configfs_subsystem *subsys;
  551. target = devm_kzalloc(&pdev->dev, sizeof(*target), GFP_KERNEL);
  552. if (!target) {
  553. dev_err(&pdev->dev, "out of memory\n");
  554. return -ENOMEM;
  555. }
  556. cg_item = &target->subsys.su_group.cg_item;
  557. sprintf(cg_item->ci_namebuf, "pcie_gadget.%d", pdev->id);
  558. cg_item->ci_type = &pcie_gadget_target_type;
  559. config = &target->config;
  560. /* get resource for application registers*/
  561. res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  562. config->va_app_base = devm_ioremap_resource(&pdev->dev, res0);
  563. if (IS_ERR(config->va_app_base)) {
  564. dev_err(&pdev->dev, "ioremap fail\n");
  565. return PTR_ERR(config->va_app_base);
  566. }
  567. /* get resource for dbi registers*/
  568. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  569. config->base = (void __iomem *)res1->start;
  570. config->va_dbi_base = devm_ioremap_resource(&pdev->dev, res1);
  571. if (IS_ERR(config->va_dbi_base)) {
  572. dev_err(&pdev->dev, "ioremap fail\n");
  573. return PTR_ERR(config->va_dbi_base);
  574. }
  575. platform_set_drvdata(pdev, target);
  576. irq = platform_get_irq(pdev, 0);
  577. if (irq < 0) {
  578. dev_err(&pdev->dev, "no update irq?\n");
  579. return irq;
  580. }
  581. status = devm_request_irq(&pdev->dev, irq, spear_pcie_gadget_irq,
  582. 0, pdev->name, NULL);
  583. if (status) {
  584. dev_err(&pdev->dev,
  585. "pcie gadget interrupt IRQ%d already claimed\n", irq);
  586. return status;
  587. }
  588. /* Register configfs hooks */
  589. subsys = &target->subsys;
  590. config_group_init(&subsys->su_group);
  591. mutex_init(&subsys->su_mutex);
  592. status = configfs_register_subsystem(subsys);
  593. if (status)
  594. return status;
  595. /*
  596. * init basic pcie application registers
  597. * do not enable clock if it is PCIE0.Ideally , all controller should
  598. * have been independent from others with respect to clock. But PCIE1
  599. * and 2 depends on PCIE0.So PCIE0 clk is provided during board init.
  600. */
  601. if (pdev->id == 1) {
  602. /*
  603. * Ideally CFG Clock should have been also enabled here. But
  604. * it is done currently during board init routne
  605. */
  606. clk = clk_get_sys("pcie1", NULL);
  607. if (IS_ERR(clk)) {
  608. pr_err("%s:couldn't get clk for pcie1\n", __func__);
  609. return PTR_ERR(clk);
  610. }
  611. status = clk_enable(clk);
  612. if (status) {
  613. pr_err("%s:couldn't enable clk for pcie1\n", __func__);
  614. return status;
  615. }
  616. } else if (pdev->id == 2) {
  617. /*
  618. * Ideally CFG Clock should have been also enabled here. But
  619. * it is done currently during board init routne
  620. */
  621. clk = clk_get_sys("pcie2", NULL);
  622. if (IS_ERR(clk)) {
  623. pr_err("%s:couldn't get clk for pcie2\n", __func__);
  624. return PTR_ERR(clk);
  625. }
  626. status = clk_enable(clk);
  627. if (status) {
  628. pr_err("%s:couldn't enable clk for pcie2\n", __func__);
  629. return status;
  630. }
  631. }
  632. spear13xx_pcie_device_init(config);
  633. return 0;
  634. }
  635. static int spear_pcie_gadget_remove(struct platform_device *pdev)
  636. {
  637. static struct pcie_gadget_target *target;
  638. target = platform_get_drvdata(pdev);
  639. configfs_unregister_subsystem(&target->subsys);
  640. return 0;
  641. }
  642. static void spear_pcie_gadget_shutdown(struct platform_device *pdev)
  643. {
  644. }
  645. static struct platform_driver spear_pcie_gadget_driver = {
  646. .probe = spear_pcie_gadget_probe,
  647. .remove = spear_pcie_gadget_remove,
  648. .shutdown = spear_pcie_gadget_shutdown,
  649. .driver = {
  650. .name = "pcie-gadget-spear",
  651. .bus = &platform_bus_type
  652. },
  653. };
  654. module_platform_driver(spear_pcie_gadget_driver);
  655. MODULE_ALIAS("platform:pcie-gadget-spear");
  656. MODULE_AUTHOR("Pratyush Anand");
  657. MODULE_LICENSE("GPL");