spear13xx_pcie_gadget.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 ssize_t pcie_gadget_show_link(
  189. struct spear_pcie_gadget_config *config,
  190. char *buf)
  191. {
  192. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  193. if (readl(&app_reg->app_status_1) & ((u32)1 << XMLH_LINK_UP_ID))
  194. return sprintf(buf, "UP");
  195. else
  196. return sprintf(buf, "DOWN");
  197. }
  198. static ssize_t pcie_gadget_store_link(
  199. struct spear_pcie_gadget_config *config,
  200. const char *buf, size_t count)
  201. {
  202. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  203. if (sysfs_streq(buf, "UP"))
  204. writel(readl(&app_reg->app_ctrl_0) | (1 << APP_LTSSM_ENABLE_ID),
  205. &app_reg->app_ctrl_0);
  206. else if (sysfs_streq(buf, "DOWN"))
  207. writel(readl(&app_reg->app_ctrl_0)
  208. & ~(1 << APP_LTSSM_ENABLE_ID),
  209. &app_reg->app_ctrl_0);
  210. else
  211. return -EINVAL;
  212. return count;
  213. }
  214. static ssize_t pcie_gadget_show_int_type(
  215. struct spear_pcie_gadget_config *config,
  216. char *buf)
  217. {
  218. return sprintf(buf, "%s", config->int_type);
  219. }
  220. static ssize_t pcie_gadget_store_int_type(
  221. struct spear_pcie_gadget_config *config,
  222. const char *buf, size_t count)
  223. {
  224. u32 cap, vec, flags;
  225. ulong vector;
  226. if (sysfs_streq(buf, "INTA"))
  227. spear_dbi_write_reg(config, PCI_INTERRUPT_LINE, 1, 1);
  228. else if (sysfs_streq(buf, "MSI")) {
  229. vector = config->requested_msi;
  230. vec = 0;
  231. while (vector > 1) {
  232. vector /= 2;
  233. vec++;
  234. }
  235. spear_dbi_write_reg(config, PCI_INTERRUPT_LINE, 1, 0);
  236. cap = pci_find_own_capability(config, PCI_CAP_ID_MSI);
  237. spear_dbi_read_reg(config, cap + PCI_MSI_FLAGS, 1, &flags);
  238. flags &= ~PCI_MSI_FLAGS_QMASK;
  239. flags |= vec << 1;
  240. spear_dbi_write_reg(config, cap + PCI_MSI_FLAGS, 1, flags);
  241. } else
  242. return -EINVAL;
  243. strcpy(config->int_type, buf);
  244. return count;
  245. }
  246. static ssize_t pcie_gadget_show_no_of_msi(
  247. struct spear_pcie_gadget_config *config,
  248. char *buf)
  249. {
  250. struct pcie_app_reg __iomem *app_reg = config->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_store_no_of_msi(
  269. struct spear_pcie_gadget_config *config,
  270. const char *buf, size_t count)
  271. {
  272. int ret;
  273. ret = kstrtoul(buf, 0, &config->requested_msi);
  274. if (ret)
  275. return ret;
  276. if (config->requested_msi > 32)
  277. config->requested_msi = 32;
  278. return count;
  279. }
  280. static ssize_t pcie_gadget_store_inta(
  281. struct spear_pcie_gadget_config *config,
  282. const char *buf, size_t count)
  283. {
  284. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  285. ulong en;
  286. int ret;
  287. ret = kstrtoul(buf, 0, &en);
  288. if (ret)
  289. return ret;
  290. if (en)
  291. writel(readl(&app_reg->app_ctrl_0) | (1 << SYS_INT_ID),
  292. &app_reg->app_ctrl_0);
  293. else
  294. writel(readl(&app_reg->app_ctrl_0) & ~(1 << SYS_INT_ID),
  295. &app_reg->app_ctrl_0);
  296. return count;
  297. }
  298. static ssize_t pcie_gadget_store_send_msi(
  299. struct spear_pcie_gadget_config *config,
  300. const char *buf, size_t count)
  301. {
  302. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  303. ulong vector;
  304. u32 ven_msi;
  305. int ret;
  306. ret = kstrtoul(buf, 0, &vector);
  307. if (ret)
  308. return ret;
  309. if (!config->configured_msi)
  310. return -EINVAL;
  311. if (vector >= config->configured_msi)
  312. return -EINVAL;
  313. ven_msi = readl(&app_reg->ven_msi_1);
  314. ven_msi &= ~VEN_MSI_FUN_NUM_MASK;
  315. ven_msi |= 0 << VEN_MSI_FUN_NUM_ID;
  316. ven_msi &= ~VEN_MSI_TC_MASK;
  317. ven_msi |= 0 << VEN_MSI_TC_ID;
  318. ven_msi &= ~VEN_MSI_VECTOR_MASK;
  319. ven_msi |= vector << VEN_MSI_VECTOR_ID;
  320. /* generating interrupt for msi vector */
  321. ven_msi |= VEN_MSI_REQ_EN;
  322. writel(ven_msi, &app_reg->ven_msi_1);
  323. udelay(1);
  324. ven_msi &= ~VEN_MSI_REQ_EN;
  325. writel(ven_msi, &app_reg->ven_msi_1);
  326. return count;
  327. }
  328. static ssize_t pcie_gadget_show_vendor_id(
  329. struct spear_pcie_gadget_config *config,
  330. char *buf)
  331. {
  332. u32 id;
  333. spear_dbi_read_reg(config, PCI_VENDOR_ID, 2, &id);
  334. return sprintf(buf, "%x", id);
  335. }
  336. static ssize_t pcie_gadget_store_vendor_id(
  337. struct spear_pcie_gadget_config *config,
  338. const char *buf, size_t count)
  339. {
  340. ulong id;
  341. int ret;
  342. ret = kstrtoul(buf, 0, &id);
  343. if (ret)
  344. return ret;
  345. spear_dbi_write_reg(config, PCI_VENDOR_ID, 2, id);
  346. return count;
  347. }
  348. static ssize_t pcie_gadget_show_device_id(
  349. struct spear_pcie_gadget_config *config,
  350. char *buf)
  351. {
  352. u32 id;
  353. spear_dbi_read_reg(config, PCI_DEVICE_ID, 2, &id);
  354. return sprintf(buf, "%x", id);
  355. }
  356. static ssize_t pcie_gadget_store_device_id(
  357. struct spear_pcie_gadget_config *config,
  358. const char *buf, size_t count)
  359. {
  360. ulong id;
  361. int ret;
  362. ret = kstrtoul(buf, 0, &id);
  363. if (ret)
  364. return ret;
  365. spear_dbi_write_reg(config, PCI_DEVICE_ID, 2, id);
  366. return count;
  367. }
  368. static ssize_t pcie_gadget_show_bar0_size(
  369. struct spear_pcie_gadget_config *config,
  370. char *buf)
  371. {
  372. return sprintf(buf, "%lx", config->bar0_size);
  373. }
  374. static ssize_t pcie_gadget_store_bar0_size(
  375. struct spear_pcie_gadget_config *config,
  376. const char *buf, size_t count)
  377. {
  378. ulong size;
  379. u32 pos, pos1;
  380. u32 no_of_bit = 0;
  381. int ret;
  382. ret = kstrtoul(buf, 0, &size);
  383. if (ret)
  384. return ret;
  385. /* min bar size is 256 */
  386. if (size <= 0x100)
  387. size = 0x100;
  388. /* max bar size is 1MB*/
  389. else if (size >= 0x100000)
  390. size = 0x100000;
  391. else {
  392. pos = 0;
  393. pos1 = 0;
  394. while (pos < 21) {
  395. pos = find_next_bit((ulong *)&size, 21, pos);
  396. if (pos != 21)
  397. pos1 = pos + 1;
  398. pos++;
  399. no_of_bit++;
  400. }
  401. if (no_of_bit == 2)
  402. pos1--;
  403. size = 1 << pos1;
  404. }
  405. config->bar0_size = size;
  406. spear_dbi_write_reg(config, PCIE_BAR0_MASK_REG, 4, size - 1);
  407. return count;
  408. }
  409. static ssize_t pcie_gadget_show_bar0_address(
  410. struct spear_pcie_gadget_config *config,
  411. char *buf)
  412. {
  413. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  414. u32 address = readl(&app_reg->pim0_mem_addr_start);
  415. return sprintf(buf, "%x", address);
  416. }
  417. static ssize_t pcie_gadget_store_bar0_address(
  418. struct spear_pcie_gadget_config *config,
  419. const char *buf, size_t count)
  420. {
  421. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  422. ulong address;
  423. int ret;
  424. ret = kstrtoul(buf, 0, &address);
  425. if (ret)
  426. return ret;
  427. address &= ~(config->bar0_size - 1);
  428. if (config->va_bar0_address)
  429. iounmap(config->va_bar0_address);
  430. config->va_bar0_address = ioremap(address, config->bar0_size);
  431. if (!config->va_bar0_address)
  432. return -ENOMEM;
  433. writel(address, &app_reg->pim0_mem_addr_start);
  434. return count;
  435. }
  436. static ssize_t pcie_gadget_show_bar0_rw_offset(
  437. struct spear_pcie_gadget_config *config,
  438. char *buf)
  439. {
  440. return sprintf(buf, "%lx", config->bar0_rw_offset);
  441. }
  442. static ssize_t pcie_gadget_store_bar0_rw_offset(
  443. struct spear_pcie_gadget_config *config,
  444. const char *buf, size_t count)
  445. {
  446. ulong offset;
  447. int ret;
  448. ret = kstrtoul(buf, 0, &offset);
  449. if (ret)
  450. return ret;
  451. if (offset % 4)
  452. return -EINVAL;
  453. config->bar0_rw_offset = offset;
  454. return count;
  455. }
  456. static ssize_t pcie_gadget_show_bar0_data(
  457. struct spear_pcie_gadget_config *config,
  458. char *buf)
  459. {
  460. ulong data;
  461. if (!config->va_bar0_address)
  462. return -ENOMEM;
  463. data = readl((ulong)config->va_bar0_address + config->bar0_rw_offset);
  464. return sprintf(buf, "%lx", data);
  465. }
  466. static ssize_t pcie_gadget_store_bar0_data(
  467. struct spear_pcie_gadget_config *config,
  468. const char *buf, size_t count)
  469. {
  470. ulong data;
  471. int ret;
  472. ret = kstrtoul(buf, 0, &data);
  473. if (ret)
  474. return ret;
  475. if (!config->va_bar0_address)
  476. return -ENOMEM;
  477. writel(data, (ulong)config->va_bar0_address + config->bar0_rw_offset);
  478. return count;
  479. }
  480. /*
  481. * Attribute definitions.
  482. */
  483. #define PCIE_GADGET_TARGET_ATTR_RO(_name) \
  484. static struct pcie_gadget_target_attr pcie_gadget_target_##_name = \
  485. __CONFIGFS_ATTR(_name, S_IRUGO, pcie_gadget_show_##_name, NULL)
  486. #define PCIE_GADGET_TARGET_ATTR_WO(_name) \
  487. static struct pcie_gadget_target_attr pcie_gadget_target_##_name = \
  488. __CONFIGFS_ATTR(_name, S_IWUSR, NULL, pcie_gadget_store_##_name)
  489. #define PCIE_GADGET_TARGET_ATTR_RW(_name) \
  490. static struct pcie_gadget_target_attr pcie_gadget_target_##_name = \
  491. __CONFIGFS_ATTR(_name, S_IRUGO | S_IWUSR, pcie_gadget_show_##_name, \
  492. pcie_gadget_store_##_name)
  493. PCIE_GADGET_TARGET_ATTR_RW(link);
  494. PCIE_GADGET_TARGET_ATTR_RW(int_type);
  495. PCIE_GADGET_TARGET_ATTR_RW(no_of_msi);
  496. PCIE_GADGET_TARGET_ATTR_WO(inta);
  497. PCIE_GADGET_TARGET_ATTR_WO(send_msi);
  498. PCIE_GADGET_TARGET_ATTR_RW(vendor_id);
  499. PCIE_GADGET_TARGET_ATTR_RW(device_id);
  500. PCIE_GADGET_TARGET_ATTR_RW(bar0_size);
  501. PCIE_GADGET_TARGET_ATTR_RW(bar0_address);
  502. PCIE_GADGET_TARGET_ATTR_RW(bar0_rw_offset);
  503. PCIE_GADGET_TARGET_ATTR_RW(bar0_data);
  504. static struct configfs_attribute *pcie_gadget_target_attrs[] = {
  505. &pcie_gadget_target_link.attr,
  506. &pcie_gadget_target_int_type.attr,
  507. &pcie_gadget_target_no_of_msi.attr,
  508. &pcie_gadget_target_inta.attr,
  509. &pcie_gadget_target_send_msi.attr,
  510. &pcie_gadget_target_vendor_id.attr,
  511. &pcie_gadget_target_device_id.attr,
  512. &pcie_gadget_target_bar0_size.attr,
  513. &pcie_gadget_target_bar0_address.attr,
  514. &pcie_gadget_target_bar0_rw_offset.attr,
  515. &pcie_gadget_target_bar0_data.attr,
  516. NULL,
  517. };
  518. static struct pcie_gadget_target *to_target(struct config_item *item)
  519. {
  520. return item ?
  521. container_of(to_configfs_subsystem(to_config_group(item)),
  522. struct pcie_gadget_target, subsys) : NULL;
  523. }
  524. /*
  525. * Item operations and type for pcie_gadget_target.
  526. */
  527. static ssize_t pcie_gadget_target_attr_show(struct config_item *item,
  528. struct configfs_attribute *attr,
  529. char *buf)
  530. {
  531. ssize_t ret = -EINVAL;
  532. struct pcie_gadget_target *target = to_target(item);
  533. struct pcie_gadget_target_attr *t_attr =
  534. container_of(attr, struct pcie_gadget_target_attr, attr);
  535. if (t_attr->show)
  536. ret = t_attr->show(&target->config, buf);
  537. return ret;
  538. }
  539. static ssize_t pcie_gadget_target_attr_store(struct config_item *item,
  540. struct configfs_attribute *attr,
  541. const char *buf,
  542. size_t count)
  543. {
  544. ssize_t ret = -EINVAL;
  545. struct pcie_gadget_target *target = to_target(item);
  546. struct pcie_gadget_target_attr *t_attr =
  547. container_of(attr, struct pcie_gadget_target_attr, attr);
  548. if (t_attr->store)
  549. ret = t_attr->store(&target->config, buf, count);
  550. return ret;
  551. }
  552. static struct configfs_item_operations pcie_gadget_target_item_ops = {
  553. .show_attribute = pcie_gadget_target_attr_show,
  554. .store_attribute = pcie_gadget_target_attr_store,
  555. };
  556. static struct config_item_type pcie_gadget_target_type = {
  557. .ct_attrs = pcie_gadget_target_attrs,
  558. .ct_item_ops = &pcie_gadget_target_item_ops,
  559. .ct_owner = THIS_MODULE,
  560. };
  561. static void spear13xx_pcie_device_init(struct spear_pcie_gadget_config *config)
  562. {
  563. struct pcie_app_reg __iomem *app_reg = config->va_app_base;
  564. /*setup registers for outbound translation */
  565. writel(config->base, &app_reg->in0_mem_addr_start);
  566. writel(app_reg->in0_mem_addr_start + IN0_MEM_SIZE,
  567. &app_reg->in0_mem_addr_limit);
  568. writel(app_reg->in0_mem_addr_limit + 1, &app_reg->in1_mem_addr_start);
  569. writel(app_reg->in1_mem_addr_start + IN1_MEM_SIZE,
  570. &app_reg->in1_mem_addr_limit);
  571. writel(app_reg->in1_mem_addr_limit + 1, &app_reg->in_io_addr_start);
  572. writel(app_reg->in_io_addr_start + IN_IO_SIZE,
  573. &app_reg->in_io_addr_limit);
  574. writel(app_reg->in_io_addr_limit + 1, &app_reg->in_cfg0_addr_start);
  575. writel(app_reg->in_cfg0_addr_start + IN_CFG0_SIZE,
  576. &app_reg->in_cfg0_addr_limit);
  577. writel(app_reg->in_cfg0_addr_limit + 1, &app_reg->in_cfg1_addr_start);
  578. writel(app_reg->in_cfg1_addr_start + IN_CFG1_SIZE,
  579. &app_reg->in_cfg1_addr_limit);
  580. writel(app_reg->in_cfg1_addr_limit + 1, &app_reg->in_msg_addr_start);
  581. writel(app_reg->in_msg_addr_start + IN_MSG_SIZE,
  582. &app_reg->in_msg_addr_limit);
  583. writel(app_reg->in0_mem_addr_start, &app_reg->pom0_mem_addr_start);
  584. writel(app_reg->in1_mem_addr_start, &app_reg->pom1_mem_addr_start);
  585. writel(app_reg->in_io_addr_start, &app_reg->pom_io_addr_start);
  586. /*setup registers for inbound translation */
  587. /* Keep AORAM mapped at BAR0 as default */
  588. config->bar0_size = INBOUND_ADDR_MASK + 1;
  589. spear_dbi_write_reg(config, PCIE_BAR0_MASK_REG, 4, INBOUND_ADDR_MASK);
  590. spear_dbi_write_reg(config, PCI_BASE_ADDRESS_0, 4, 0xC);
  591. config->va_bar0_address = ioremap(SPEAR13XX_SYSRAM1_BASE,
  592. config->bar0_size);
  593. writel(SPEAR13XX_SYSRAM1_BASE, &app_reg->pim0_mem_addr_start);
  594. writel(0, &app_reg->pim1_mem_addr_start);
  595. writel(INBOUND_ADDR_MASK + 1, &app_reg->mem0_addr_offset_limit);
  596. writel(0x0, &app_reg->pim_io_addr_start);
  597. writel(0x0, &app_reg->pim_io_addr_start);
  598. writel(0x0, &app_reg->pim_rom_addr_start);
  599. writel(DEVICE_TYPE_EP | (1 << MISCTRL_EN_ID)
  600. | ((u32)1 << REG_TRANSLATION_ENABLE),
  601. &app_reg->app_ctrl_0);
  602. /* disable all rx interrupts */
  603. writel(0, &app_reg->int_mask);
  604. /* Select INTA as default*/
  605. spear_dbi_write_reg(config, PCI_INTERRUPT_LINE, 1, 1);
  606. }
  607. static int spear_pcie_gadget_probe(struct platform_device *pdev)
  608. {
  609. struct resource *res0, *res1;
  610. unsigned int status = 0;
  611. int irq;
  612. struct clk *clk;
  613. static struct pcie_gadget_target *target;
  614. struct spear_pcie_gadget_config *config;
  615. struct config_item *cg_item;
  616. struct configfs_subsystem *subsys;
  617. target = devm_kzalloc(&pdev->dev, sizeof(*target), GFP_KERNEL);
  618. if (!target) {
  619. dev_err(&pdev->dev, "out of memory\n");
  620. return -ENOMEM;
  621. }
  622. cg_item = &target->subsys.su_group.cg_item;
  623. sprintf(cg_item->ci_namebuf, "pcie_gadget.%d", pdev->id);
  624. cg_item->ci_type = &pcie_gadget_target_type;
  625. config = &target->config;
  626. /* get resource for application registers*/
  627. res0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  628. config->va_app_base = devm_ioremap_resource(&pdev->dev, res0);
  629. if (IS_ERR(config->va_app_base)) {
  630. dev_err(&pdev->dev, "ioremap fail\n");
  631. return PTR_ERR(config->va_app_base);
  632. }
  633. /* get resource for dbi registers*/
  634. res1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  635. config->base = (void __iomem *)res1->start;
  636. config->va_dbi_base = devm_ioremap_resource(&pdev->dev, res1);
  637. if (IS_ERR(config->va_dbi_base)) {
  638. dev_err(&pdev->dev, "ioremap fail\n");
  639. return PTR_ERR(config->va_dbi_base);
  640. }
  641. platform_set_drvdata(pdev, target);
  642. irq = platform_get_irq(pdev, 0);
  643. if (irq < 0) {
  644. dev_err(&pdev->dev, "no update irq?\n");
  645. return irq;
  646. }
  647. status = devm_request_irq(&pdev->dev, irq, spear_pcie_gadget_irq,
  648. 0, pdev->name, NULL);
  649. if (status) {
  650. dev_err(&pdev->dev,
  651. "pcie gadget interrupt IRQ%d already claimed\n", irq);
  652. return status;
  653. }
  654. /* Register configfs hooks */
  655. subsys = &target->subsys;
  656. config_group_init(&subsys->su_group);
  657. mutex_init(&subsys->su_mutex);
  658. status = configfs_register_subsystem(subsys);
  659. if (status)
  660. return status;
  661. /*
  662. * init basic pcie application registers
  663. * do not enable clock if it is PCIE0.Ideally , all controller should
  664. * have been independent from others with respect to clock. But PCIE1
  665. * and 2 depends on PCIE0.So PCIE0 clk is provided during board init.
  666. */
  667. if (pdev->id == 1) {
  668. /*
  669. * Ideally CFG Clock should have been also enabled here. But
  670. * it is done currently during board init routne
  671. */
  672. clk = clk_get_sys("pcie1", NULL);
  673. if (IS_ERR(clk)) {
  674. pr_err("%s:couldn't get clk for pcie1\n", __func__);
  675. return PTR_ERR(clk);
  676. }
  677. status = clk_enable(clk);
  678. if (status) {
  679. pr_err("%s:couldn't enable clk for pcie1\n", __func__);
  680. return status;
  681. }
  682. } else if (pdev->id == 2) {
  683. /*
  684. * Ideally CFG Clock should have been also enabled here. But
  685. * it is done currently during board init routne
  686. */
  687. clk = clk_get_sys("pcie2", NULL);
  688. if (IS_ERR(clk)) {
  689. pr_err("%s:couldn't get clk for pcie2\n", __func__);
  690. return PTR_ERR(clk);
  691. }
  692. status = clk_enable(clk);
  693. if (status) {
  694. pr_err("%s:couldn't enable clk for pcie2\n", __func__);
  695. return status;
  696. }
  697. }
  698. spear13xx_pcie_device_init(config);
  699. return 0;
  700. }
  701. static int spear_pcie_gadget_remove(struct platform_device *pdev)
  702. {
  703. static struct pcie_gadget_target *target;
  704. target = platform_get_drvdata(pdev);
  705. configfs_unregister_subsystem(&target->subsys);
  706. return 0;
  707. }
  708. static void spear_pcie_gadget_shutdown(struct platform_device *pdev)
  709. {
  710. }
  711. static struct platform_driver spear_pcie_gadget_driver = {
  712. .probe = spear_pcie_gadget_probe,
  713. .remove = spear_pcie_gadget_remove,
  714. .shutdown = spear_pcie_gadget_shutdown,
  715. .driver = {
  716. .name = "pcie-gadget-spear",
  717. .bus = &platform_bus_type
  718. },
  719. };
  720. module_platform_driver(spear_pcie_gadget_driver);
  721. MODULE_ALIAS("platform:pcie-gadget-spear");
  722. MODULE_AUTHOR("Pratyush Anand");
  723. MODULE_LICENSE("GPL");