vlynq.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. /*
  2. * Copyright (C) 2006, 2007 Eugene Konev <ejka@openwrt.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. * Parts of the VLYNQ specification can be found here:
  19. * http://www.ti.com/litv/pdf/sprue36a
  20. */
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/kernel.h>
  24. #include <linux/string.h>
  25. #include <linux/device.h>
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/device.h>
  31. #include <linux/delay.h>
  32. #include <linux/io.h>
  33. #include <linux/vlynq.h>
  34. #define VLYNQ_CTRL_PM_ENABLE 0x80000000
  35. #define VLYNQ_CTRL_CLOCK_INT 0x00008000
  36. #define VLYNQ_CTRL_CLOCK_DIV(x) (((x) & 7) << 16)
  37. #define VLYNQ_CTRL_INT_LOCAL 0x00004000
  38. #define VLYNQ_CTRL_INT_ENABLE 0x00002000
  39. #define VLYNQ_CTRL_INT_VECTOR(x) (((x) & 0x1f) << 8)
  40. #define VLYNQ_CTRL_INT2CFG 0x00000080
  41. #define VLYNQ_CTRL_RESET 0x00000001
  42. #define VLYNQ_CTRL_CLOCK_MASK (0x7 << 16)
  43. #define VLYNQ_INT_OFFSET 0x00000014
  44. #define VLYNQ_REMOTE_OFFSET 0x00000080
  45. #define VLYNQ_STATUS_LINK 0x00000001
  46. #define VLYNQ_STATUS_LERROR 0x00000080
  47. #define VLYNQ_STATUS_RERROR 0x00000100
  48. #define VINT_ENABLE 0x00000100
  49. #define VINT_TYPE_EDGE 0x00000080
  50. #define VINT_LEVEL_LOW 0x00000040
  51. #define VINT_VECTOR(x) ((x) & 0x1f)
  52. #define VINT_OFFSET(irq) (8 * ((irq) % 4))
  53. #define VLYNQ_AUTONEGO_V2 0x00010000
  54. struct vlynq_regs {
  55. u32 revision;
  56. u32 control;
  57. u32 status;
  58. u32 int_prio;
  59. u32 int_status;
  60. u32 int_pending;
  61. u32 int_ptr;
  62. u32 tx_offset;
  63. struct vlynq_mapping rx_mapping[4];
  64. u32 chip;
  65. u32 autonego;
  66. u32 unused[6];
  67. u32 int_device[8];
  68. };
  69. #ifdef CONFIG_VLYNQ_DEBUG
  70. static void vlynq_dump_regs(struct vlynq_device *dev)
  71. {
  72. int i;
  73. printk(KERN_DEBUG "VLYNQ local=%p remote=%p\n",
  74. dev->local, dev->remote);
  75. for (i = 0; i < 32; i++) {
  76. printk(KERN_DEBUG "VLYNQ: local %d: %08x\n",
  77. i + 1, ((u32 *)dev->local)[i]);
  78. printk(KERN_DEBUG "VLYNQ: remote %d: %08x\n",
  79. i + 1, ((u32 *)dev->remote)[i]);
  80. }
  81. }
  82. static void vlynq_dump_mem(u32 *base, int count)
  83. {
  84. int i;
  85. for (i = 0; i < (count + 3) / 4; i++) {
  86. if (i % 4 == 0)
  87. printk(KERN_DEBUG "\nMEM[0x%04x]:", i * 4);
  88. printk(KERN_DEBUG " 0x%08x", *(base + i));
  89. }
  90. printk(KERN_DEBUG "\n");
  91. }
  92. #endif
  93. /* Check the VLYNQ link status with a given device */
  94. static int vlynq_linked(struct vlynq_device *dev)
  95. {
  96. int i;
  97. for (i = 0; i < 100; i++)
  98. if (readl(&dev->local->status) & VLYNQ_STATUS_LINK)
  99. return 1;
  100. else
  101. cpu_relax();
  102. return 0;
  103. }
  104. static void vlynq_reset(struct vlynq_device *dev)
  105. {
  106. writel(readl(&dev->local->control) | VLYNQ_CTRL_RESET,
  107. &dev->local->control);
  108. /* Wait for the devices to finish resetting */
  109. msleep(5);
  110. /* Remove reset bit */
  111. writel(readl(&dev->local->control) & ~VLYNQ_CTRL_RESET,
  112. &dev->local->control);
  113. /* Give some time for the devices to settle */
  114. msleep(5);
  115. }
  116. static void vlynq_irq_unmask(unsigned int irq)
  117. {
  118. u32 val;
  119. struct vlynq_device *dev = get_irq_chip_data(irq);
  120. int virq;
  121. BUG_ON(!dev);
  122. virq = irq - dev->irq_start;
  123. val = readl(&dev->remote->int_device[virq >> 2]);
  124. val |= (VINT_ENABLE | virq) << VINT_OFFSET(virq);
  125. writel(val, &dev->remote->int_device[virq >> 2]);
  126. }
  127. static void vlynq_irq_mask(unsigned int irq)
  128. {
  129. u32 val;
  130. struct vlynq_device *dev = get_irq_chip_data(irq);
  131. int virq;
  132. BUG_ON(!dev);
  133. virq = irq - dev->irq_start;
  134. val = readl(&dev->remote->int_device[virq >> 2]);
  135. val &= ~(VINT_ENABLE << VINT_OFFSET(virq));
  136. writel(val, &dev->remote->int_device[virq >> 2]);
  137. }
  138. static int vlynq_irq_type(unsigned int irq, unsigned int flow_type)
  139. {
  140. u32 val;
  141. struct vlynq_device *dev = get_irq_chip_data(irq);
  142. int virq;
  143. BUG_ON(!dev);
  144. virq = irq - dev->irq_start;
  145. val = readl(&dev->remote->int_device[virq >> 2]);
  146. switch (flow_type & IRQ_TYPE_SENSE_MASK) {
  147. case IRQ_TYPE_EDGE_RISING:
  148. case IRQ_TYPE_EDGE_FALLING:
  149. case IRQ_TYPE_EDGE_BOTH:
  150. val |= VINT_TYPE_EDGE << VINT_OFFSET(virq);
  151. val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
  152. break;
  153. case IRQ_TYPE_LEVEL_HIGH:
  154. val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
  155. val &= ~(VINT_LEVEL_LOW << VINT_OFFSET(virq));
  156. break;
  157. case IRQ_TYPE_LEVEL_LOW:
  158. val &= ~(VINT_TYPE_EDGE << VINT_OFFSET(virq));
  159. val |= VINT_LEVEL_LOW << VINT_OFFSET(virq);
  160. break;
  161. default:
  162. return -EINVAL;
  163. }
  164. writel(val, &dev->remote->int_device[virq >> 2]);
  165. return 0;
  166. }
  167. static void vlynq_local_ack(unsigned int irq)
  168. {
  169. struct vlynq_device *dev = get_irq_chip_data(irq);
  170. u32 status = readl(&dev->local->status);
  171. pr_debug("%s: local status: 0x%08x\n",
  172. dev_name(&dev->dev), status);
  173. writel(status, &dev->local->status);
  174. }
  175. static void vlynq_remote_ack(unsigned int irq)
  176. {
  177. struct vlynq_device *dev = get_irq_chip_data(irq);
  178. u32 status = readl(&dev->remote->status);
  179. pr_debug("%s: remote status: 0x%08x\n",
  180. dev_name(&dev->dev), status);
  181. writel(status, &dev->remote->status);
  182. }
  183. static irqreturn_t vlynq_irq(int irq, void *dev_id)
  184. {
  185. struct vlynq_device *dev = dev_id;
  186. u32 status;
  187. int virq = 0;
  188. status = readl(&dev->local->int_status);
  189. writel(status, &dev->local->int_status);
  190. if (unlikely(!status))
  191. spurious_interrupt();
  192. while (status) {
  193. if (status & 1)
  194. do_IRQ(dev->irq_start + virq);
  195. status >>= 1;
  196. virq++;
  197. }
  198. return IRQ_HANDLED;
  199. }
  200. static struct irq_chip vlynq_irq_chip = {
  201. .name = "vlynq",
  202. .unmask = vlynq_irq_unmask,
  203. .mask = vlynq_irq_mask,
  204. .set_type = vlynq_irq_type,
  205. };
  206. static struct irq_chip vlynq_local_chip = {
  207. .name = "vlynq local error",
  208. .unmask = vlynq_irq_unmask,
  209. .mask = vlynq_irq_mask,
  210. .ack = vlynq_local_ack,
  211. };
  212. static struct irq_chip vlynq_remote_chip = {
  213. .name = "vlynq local error",
  214. .unmask = vlynq_irq_unmask,
  215. .mask = vlynq_irq_mask,
  216. .ack = vlynq_remote_ack,
  217. };
  218. static int vlynq_setup_irq(struct vlynq_device *dev)
  219. {
  220. u32 val;
  221. int i, virq;
  222. if (dev->local_irq == dev->remote_irq) {
  223. printk(KERN_ERR
  224. "%s: local vlynq irq should be different from remote\n",
  225. dev_name(&dev->dev));
  226. return -EINVAL;
  227. }
  228. /* Clear local and remote error bits */
  229. writel(readl(&dev->local->status), &dev->local->status);
  230. writel(readl(&dev->remote->status), &dev->remote->status);
  231. /* Now setup interrupts */
  232. val = VLYNQ_CTRL_INT_VECTOR(dev->local_irq);
  233. val |= VLYNQ_CTRL_INT_ENABLE | VLYNQ_CTRL_INT_LOCAL |
  234. VLYNQ_CTRL_INT2CFG;
  235. val |= readl(&dev->local->control);
  236. writel(VLYNQ_INT_OFFSET, &dev->local->int_ptr);
  237. writel(val, &dev->local->control);
  238. val = VLYNQ_CTRL_INT_VECTOR(dev->remote_irq);
  239. val |= VLYNQ_CTRL_INT_ENABLE;
  240. val |= readl(&dev->remote->control);
  241. writel(VLYNQ_INT_OFFSET, &dev->remote->int_ptr);
  242. writel(val, &dev->remote->int_ptr);
  243. writel(val, &dev->remote->control);
  244. for (i = dev->irq_start; i <= dev->irq_end; i++) {
  245. virq = i - dev->irq_start;
  246. if (virq == dev->local_irq) {
  247. set_irq_chip_and_handler(i, &vlynq_local_chip,
  248. handle_level_irq);
  249. set_irq_chip_data(i, dev);
  250. } else if (virq == dev->remote_irq) {
  251. set_irq_chip_and_handler(i, &vlynq_remote_chip,
  252. handle_level_irq);
  253. set_irq_chip_data(i, dev);
  254. } else {
  255. set_irq_chip_and_handler(i, &vlynq_irq_chip,
  256. handle_simple_irq);
  257. set_irq_chip_data(i, dev);
  258. writel(0, &dev->remote->int_device[virq >> 2]);
  259. }
  260. }
  261. if (request_irq(dev->irq, vlynq_irq, IRQF_SHARED, "vlynq", dev)) {
  262. printk(KERN_ERR "%s: request_irq failed\n",
  263. dev_name(&dev->dev));
  264. return -EAGAIN;
  265. }
  266. return 0;
  267. }
  268. static void vlynq_device_release(struct device *dev)
  269. {
  270. struct vlynq_device *vdev = to_vlynq_device(dev);
  271. kfree(vdev);
  272. }
  273. static int vlynq_device_match(struct device *dev,
  274. struct device_driver *drv)
  275. {
  276. struct vlynq_device *vdev = to_vlynq_device(dev);
  277. struct vlynq_driver *vdrv = to_vlynq_driver(drv);
  278. struct vlynq_device_id *ids = vdrv->id_table;
  279. while (ids->id) {
  280. if (ids->id == vdev->dev_id) {
  281. vdev->divisor = ids->divisor;
  282. vlynq_set_drvdata(vdev, ids);
  283. printk(KERN_INFO "Driver found for VLYNQ "
  284. "device: %08x\n", vdev->dev_id);
  285. return 1;
  286. }
  287. printk(KERN_DEBUG "Not using the %08x VLYNQ device's driver"
  288. " for VLYNQ device: %08x\n", ids->id, vdev->dev_id);
  289. ids++;
  290. }
  291. return 0;
  292. }
  293. static int vlynq_device_probe(struct device *dev)
  294. {
  295. struct vlynq_device *vdev = to_vlynq_device(dev);
  296. struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
  297. struct vlynq_device_id *id = vlynq_get_drvdata(vdev);
  298. int result = -ENODEV;
  299. if (drv->probe)
  300. result = drv->probe(vdev, id);
  301. if (result)
  302. put_device(dev);
  303. return result;
  304. }
  305. static int vlynq_device_remove(struct device *dev)
  306. {
  307. struct vlynq_driver *drv = to_vlynq_driver(dev->driver);
  308. if (drv->remove)
  309. drv->remove(to_vlynq_device(dev));
  310. return 0;
  311. }
  312. int __vlynq_register_driver(struct vlynq_driver *driver, struct module *owner)
  313. {
  314. driver->driver.name = driver->name;
  315. driver->driver.bus = &vlynq_bus_type;
  316. return driver_register(&driver->driver);
  317. }
  318. EXPORT_SYMBOL(__vlynq_register_driver);
  319. void vlynq_unregister_driver(struct vlynq_driver *driver)
  320. {
  321. driver_unregister(&driver->driver);
  322. }
  323. EXPORT_SYMBOL(vlynq_unregister_driver);
  324. /*
  325. * A VLYNQ remote device can clock the VLYNQ bus master
  326. * using a dedicated clock line. In that case, both the
  327. * remove device and the bus master should have the same
  328. * serial clock dividers configured. Iterate through the
  329. * 8 possible dividers until we actually link with the
  330. * device.
  331. */
  332. static int __vlynq_try_remote(struct vlynq_device *dev)
  333. {
  334. int i;
  335. vlynq_reset(dev);
  336. for (i = dev->dev_id ? vlynq_rdiv2 : vlynq_rdiv8; dev->dev_id ?
  337. i <= vlynq_rdiv8 : i >= vlynq_rdiv2;
  338. dev->dev_id ? i++ : i--) {
  339. if (!vlynq_linked(dev))
  340. break;
  341. writel((readl(&dev->remote->control) &
  342. ~VLYNQ_CTRL_CLOCK_MASK) |
  343. VLYNQ_CTRL_CLOCK_INT |
  344. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
  345. &dev->remote->control);
  346. writel((readl(&dev->local->control)
  347. & ~(VLYNQ_CTRL_CLOCK_INT |
  348. VLYNQ_CTRL_CLOCK_MASK)) |
  349. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_rdiv1),
  350. &dev->local->control);
  351. if (vlynq_linked(dev)) {
  352. printk(KERN_DEBUG
  353. "%s: using remote clock divisor %d\n",
  354. dev_name(&dev->dev), i - vlynq_rdiv1 + 1);
  355. dev->divisor = i;
  356. return 0;
  357. } else {
  358. vlynq_reset(dev);
  359. }
  360. }
  361. return -ENODEV;
  362. }
  363. /*
  364. * A VLYNQ remote device can be clocked by the VLYNQ bus
  365. * master using a dedicated clock line. In that case, only
  366. * the bus master configures the serial clock divider.
  367. * Iterate through the 8 possible dividers until we
  368. * actually get a link with the device.
  369. */
  370. static int __vlynq_try_local(struct vlynq_device *dev)
  371. {
  372. int i;
  373. vlynq_reset(dev);
  374. for (i = dev->dev_id ? vlynq_ldiv2 : vlynq_ldiv8; dev->dev_id ?
  375. i <= vlynq_ldiv8 : i >= vlynq_ldiv2;
  376. dev->dev_id ? i++ : i--) {
  377. writel((readl(&dev->local->control) &
  378. ~VLYNQ_CTRL_CLOCK_MASK) |
  379. VLYNQ_CTRL_CLOCK_INT |
  380. VLYNQ_CTRL_CLOCK_DIV(i - vlynq_ldiv1),
  381. &dev->local->control);
  382. if (vlynq_linked(dev)) {
  383. printk(KERN_DEBUG
  384. "%s: using local clock divisor %d\n",
  385. dev_name(&dev->dev), i - vlynq_ldiv1 + 1);
  386. dev->divisor = i;
  387. return 0;
  388. } else {
  389. vlynq_reset(dev);
  390. }
  391. }
  392. return -ENODEV;
  393. }
  394. /*
  395. * When using external clocking method, serial clock
  396. * is supplied by an external oscillator, therefore we
  397. * should mask the local clock bit in the clock control
  398. * register for both the bus master and the remote device.
  399. */
  400. static int __vlynq_try_external(struct vlynq_device *dev)
  401. {
  402. vlynq_reset(dev);
  403. if (!vlynq_linked(dev))
  404. return -ENODEV;
  405. writel((readl(&dev->remote->control) &
  406. ~VLYNQ_CTRL_CLOCK_INT),
  407. &dev->remote->control);
  408. writel((readl(&dev->local->control) &
  409. ~VLYNQ_CTRL_CLOCK_INT),
  410. &dev->local->control);
  411. if (vlynq_linked(dev)) {
  412. printk(KERN_DEBUG "%s: using external clock\n",
  413. dev_name(&dev->dev));
  414. dev->divisor = vlynq_div_external;
  415. return 0;
  416. }
  417. return -ENODEV;
  418. }
  419. static int __vlynq_enable_device(struct vlynq_device *dev)
  420. {
  421. int result;
  422. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  423. result = ops->on(dev);
  424. if (result)
  425. return result;
  426. switch (dev->divisor) {
  427. case vlynq_div_external:
  428. case vlynq_div_auto:
  429. /* When the device is brought from reset it should have clock
  430. * generation negotiated by hardware.
  431. * Check which device is generating clocks and perform setup
  432. * accordingly */
  433. if (vlynq_linked(dev) && readl(&dev->remote->control) &
  434. VLYNQ_CTRL_CLOCK_INT) {
  435. if (!__vlynq_try_remote(dev) ||
  436. !__vlynq_try_local(dev) ||
  437. !__vlynq_try_external(dev))
  438. return 0;
  439. } else {
  440. if (!__vlynq_try_external(dev) ||
  441. !__vlynq_try_local(dev) ||
  442. !__vlynq_try_remote(dev))
  443. return 0;
  444. }
  445. break;
  446. case vlynq_ldiv1:
  447. case vlynq_ldiv2:
  448. case vlynq_ldiv3:
  449. case vlynq_ldiv4:
  450. case vlynq_ldiv5:
  451. case vlynq_ldiv6:
  452. case vlynq_ldiv7:
  453. case vlynq_ldiv8:
  454. writel(VLYNQ_CTRL_CLOCK_INT |
  455. VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
  456. vlynq_ldiv1), &dev->local->control);
  457. writel(0, &dev->remote->control);
  458. if (vlynq_linked(dev)) {
  459. printk(KERN_DEBUG
  460. "%s: using local clock divisor %d\n",
  461. dev_name(&dev->dev),
  462. dev->divisor - vlynq_ldiv1 + 1);
  463. return 0;
  464. }
  465. break;
  466. case vlynq_rdiv1:
  467. case vlynq_rdiv2:
  468. case vlynq_rdiv3:
  469. case vlynq_rdiv4:
  470. case vlynq_rdiv5:
  471. case vlynq_rdiv6:
  472. case vlynq_rdiv7:
  473. case vlynq_rdiv8:
  474. writel(0, &dev->local->control);
  475. writel(VLYNQ_CTRL_CLOCK_INT |
  476. VLYNQ_CTRL_CLOCK_DIV(dev->divisor -
  477. vlynq_rdiv1), &dev->remote->control);
  478. if (vlynq_linked(dev)) {
  479. printk(KERN_DEBUG
  480. "%s: using remote clock divisor %d\n",
  481. dev_name(&dev->dev),
  482. dev->divisor - vlynq_rdiv1 + 1);
  483. return 0;
  484. }
  485. break;
  486. }
  487. ops->off(dev);
  488. return -ENODEV;
  489. }
  490. int vlynq_enable_device(struct vlynq_device *dev)
  491. {
  492. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  493. int result = -ENODEV;
  494. result = __vlynq_enable_device(dev);
  495. if (result)
  496. return result;
  497. result = vlynq_setup_irq(dev);
  498. if (result)
  499. ops->off(dev);
  500. dev->enabled = !result;
  501. return result;
  502. }
  503. EXPORT_SYMBOL(vlynq_enable_device);
  504. void vlynq_disable_device(struct vlynq_device *dev)
  505. {
  506. struct plat_vlynq_ops *ops = dev->dev.platform_data;
  507. dev->enabled = 0;
  508. free_irq(dev->irq, dev);
  509. ops->off(dev);
  510. }
  511. EXPORT_SYMBOL(vlynq_disable_device);
  512. int vlynq_set_local_mapping(struct vlynq_device *dev, u32 tx_offset,
  513. struct vlynq_mapping *mapping)
  514. {
  515. int i;
  516. if (!dev->enabled)
  517. return -ENXIO;
  518. writel(tx_offset, &dev->local->tx_offset);
  519. for (i = 0; i < 4; i++) {
  520. writel(mapping[i].offset, &dev->local->rx_mapping[i].offset);
  521. writel(mapping[i].size, &dev->local->rx_mapping[i].size);
  522. }
  523. return 0;
  524. }
  525. EXPORT_SYMBOL(vlynq_set_local_mapping);
  526. int vlynq_set_remote_mapping(struct vlynq_device *dev, u32 tx_offset,
  527. struct vlynq_mapping *mapping)
  528. {
  529. int i;
  530. if (!dev->enabled)
  531. return -ENXIO;
  532. writel(tx_offset, &dev->remote->tx_offset);
  533. for (i = 0; i < 4; i++) {
  534. writel(mapping[i].offset, &dev->remote->rx_mapping[i].offset);
  535. writel(mapping[i].size, &dev->remote->rx_mapping[i].size);
  536. }
  537. return 0;
  538. }
  539. EXPORT_SYMBOL(vlynq_set_remote_mapping);
  540. int vlynq_set_local_irq(struct vlynq_device *dev, int virq)
  541. {
  542. int irq = dev->irq_start + virq;
  543. if (dev->enabled)
  544. return -EBUSY;
  545. if ((irq < dev->irq_start) || (irq > dev->irq_end))
  546. return -EINVAL;
  547. if (virq == dev->remote_irq)
  548. return -EINVAL;
  549. dev->local_irq = virq;
  550. return 0;
  551. }
  552. EXPORT_SYMBOL(vlynq_set_local_irq);
  553. int vlynq_set_remote_irq(struct vlynq_device *dev, int virq)
  554. {
  555. int irq = dev->irq_start + virq;
  556. if (dev->enabled)
  557. return -EBUSY;
  558. if ((irq < dev->irq_start) || (irq > dev->irq_end))
  559. return -EINVAL;
  560. if (virq == dev->local_irq)
  561. return -EINVAL;
  562. dev->remote_irq = virq;
  563. return 0;
  564. }
  565. EXPORT_SYMBOL(vlynq_set_remote_irq);
  566. static int vlynq_probe(struct platform_device *pdev)
  567. {
  568. struct vlynq_device *dev;
  569. struct resource *regs_res, *mem_res, *irq_res;
  570. int len, result;
  571. regs_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "regs");
  572. if (!regs_res)
  573. return -ENODEV;
  574. mem_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "mem");
  575. if (!mem_res)
  576. return -ENODEV;
  577. irq_res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "devirq");
  578. if (!irq_res)
  579. return -ENODEV;
  580. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  581. if (!dev) {
  582. printk(KERN_ERR
  583. "vlynq: failed to allocate device structure\n");
  584. return -ENOMEM;
  585. }
  586. dev->id = pdev->id;
  587. dev->dev.bus = &vlynq_bus_type;
  588. dev->dev.parent = &pdev->dev;
  589. dev_set_name(&dev->dev, "vlynq%d", dev->id);
  590. dev->dev.platform_data = pdev->dev.platform_data;
  591. dev->dev.release = vlynq_device_release;
  592. dev->regs_start = regs_res->start;
  593. dev->regs_end = regs_res->end;
  594. dev->mem_start = mem_res->start;
  595. dev->mem_end = mem_res->end;
  596. len = regs_res->end - regs_res->start;
  597. if (!request_mem_region(regs_res->start, len, dev_name(&dev->dev))) {
  598. printk(KERN_ERR "%s: Can't request vlynq registers\n",
  599. dev_name(&dev->dev));
  600. result = -ENXIO;
  601. goto fail_request;
  602. }
  603. dev->local = ioremap(regs_res->start, len);
  604. if (!dev->local) {
  605. printk(KERN_ERR "%s: Can't remap vlynq registers\n",
  606. dev_name(&dev->dev));
  607. result = -ENXIO;
  608. goto fail_remap;
  609. }
  610. dev->remote = (struct vlynq_regs *)((void *)dev->local +
  611. VLYNQ_REMOTE_OFFSET);
  612. dev->irq = platform_get_irq_byname(pdev, "irq");
  613. dev->irq_start = irq_res->start;
  614. dev->irq_end = irq_res->end;
  615. dev->local_irq = dev->irq_end - dev->irq_start;
  616. dev->remote_irq = dev->local_irq - 1;
  617. if (device_register(&dev->dev))
  618. goto fail_register;
  619. platform_set_drvdata(pdev, dev);
  620. printk(KERN_INFO "%s: regs 0x%p, irq %d, mem 0x%p\n",
  621. dev_name(&dev->dev), (void *)dev->regs_start, dev->irq,
  622. (void *)dev->mem_start);
  623. dev->dev_id = 0;
  624. dev->divisor = vlynq_div_auto;
  625. result = __vlynq_enable_device(dev);
  626. if (result == 0) {
  627. dev->dev_id = readl(&dev->remote->chip);
  628. ((struct plat_vlynq_ops *)(dev->dev.platform_data))->off(dev);
  629. }
  630. if (dev->dev_id)
  631. printk(KERN_INFO "Found a VLYNQ device: %08x\n", dev->dev_id);
  632. return 0;
  633. fail_register:
  634. iounmap(dev->local);
  635. fail_remap:
  636. fail_request:
  637. release_mem_region(regs_res->start, len);
  638. kfree(dev);
  639. return result;
  640. }
  641. static int vlynq_remove(struct platform_device *pdev)
  642. {
  643. struct vlynq_device *dev = platform_get_drvdata(pdev);
  644. device_unregister(&dev->dev);
  645. iounmap(dev->local);
  646. release_mem_region(dev->regs_start, dev->regs_end - dev->regs_start);
  647. kfree(dev);
  648. return 0;
  649. }
  650. static struct platform_driver vlynq_platform_driver = {
  651. .driver.name = "vlynq",
  652. .probe = vlynq_probe,
  653. .remove = __devexit_p(vlynq_remove),
  654. };
  655. struct bus_type vlynq_bus_type = {
  656. .name = "vlynq",
  657. .match = vlynq_device_match,
  658. .probe = vlynq_device_probe,
  659. .remove = vlynq_device_remove,
  660. };
  661. EXPORT_SYMBOL(vlynq_bus_type);
  662. static int __devinit vlynq_init(void)
  663. {
  664. int res = 0;
  665. res = bus_register(&vlynq_bus_type);
  666. if (res)
  667. goto fail_bus;
  668. res = platform_driver_register(&vlynq_platform_driver);
  669. if (res)
  670. goto fail_platform;
  671. return 0;
  672. fail_platform:
  673. bus_unregister(&vlynq_bus_type);
  674. fail_bus:
  675. return res;
  676. }
  677. static void __devexit vlynq_exit(void)
  678. {
  679. platform_driver_unregister(&vlynq_platform_driver);
  680. bus_unregister(&vlynq_bus_type);
  681. }
  682. module_init(vlynq_init);
  683. module_exit(vlynq_exit);