spear13xx_pcie_gadget.c 23 KB

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