vlynq.c 19 KB

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