spi-fsl-spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. * Freescale SPI controller driver.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/types.h>
  20. #include <linux/kernel.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/delay.h>
  23. #include <linux/irq.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/spi/spi_bitbang.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/fsl_devices.h>
  28. #include <linux/dma-mapping.h>
  29. #include <linux/mm.h>
  30. #include <linux/mutex.h>
  31. #include <linux/of.h>
  32. #include <linux/of_platform.h>
  33. #include <linux/of_address.h>
  34. #include <linux/of_irq.h>
  35. #include <linux/gpio.h>
  36. #include <linux/of_gpio.h>
  37. #include "spi-fsl-lib.h"
  38. #include "spi-fsl-cpm.h"
  39. #include "spi-fsl-spi.h"
  40. static void fsl_spi_change_mode(struct spi_device *spi)
  41. {
  42. struct mpc8xxx_spi *mspi = spi_master_get_devdata(spi->master);
  43. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  44. struct fsl_spi_reg *reg_base = mspi->reg_base;
  45. __be32 __iomem *mode = &reg_base->mode;
  46. unsigned long flags;
  47. if (cs->hw_mode == mpc8xxx_spi_read_reg(mode))
  48. return;
  49. /* Turn off IRQs locally to minimize time that SPI is disabled. */
  50. local_irq_save(flags);
  51. /* Turn off SPI unit prior changing mode */
  52. mpc8xxx_spi_write_reg(mode, cs->hw_mode & ~SPMODE_ENABLE);
  53. /* When in CPM mode, we need to reinit tx and rx. */
  54. if (mspi->flags & SPI_CPM_MODE) {
  55. fsl_spi_cpm_reinit_txrx(mspi);
  56. }
  57. mpc8xxx_spi_write_reg(mode, cs->hw_mode);
  58. local_irq_restore(flags);
  59. }
  60. static void fsl_spi_chipselect(struct spi_device *spi, int value)
  61. {
  62. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  63. struct fsl_spi_platform_data *pdata;
  64. bool pol = spi->mode & SPI_CS_HIGH;
  65. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  66. pdata = spi->dev.parent->parent->platform_data;
  67. if (value == BITBANG_CS_INACTIVE) {
  68. if (pdata->cs_control)
  69. pdata->cs_control(spi, !pol);
  70. }
  71. if (value == BITBANG_CS_ACTIVE) {
  72. mpc8xxx_spi->rx_shift = cs->rx_shift;
  73. mpc8xxx_spi->tx_shift = cs->tx_shift;
  74. mpc8xxx_spi->get_rx = cs->get_rx;
  75. mpc8xxx_spi->get_tx = cs->get_tx;
  76. fsl_spi_change_mode(spi);
  77. if (pdata->cs_control)
  78. pdata->cs_control(spi, pol);
  79. }
  80. }
  81. static int mspi_apply_cpu_mode_quirks(struct spi_mpc8xxx_cs *cs,
  82. struct spi_device *spi,
  83. struct mpc8xxx_spi *mpc8xxx_spi,
  84. int bits_per_word)
  85. {
  86. cs->rx_shift = 0;
  87. cs->tx_shift = 0;
  88. if (bits_per_word <= 8) {
  89. cs->get_rx = mpc8xxx_spi_rx_buf_u8;
  90. cs->get_tx = mpc8xxx_spi_tx_buf_u8;
  91. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  92. cs->rx_shift = 16;
  93. cs->tx_shift = 24;
  94. }
  95. } else if (bits_per_word <= 16) {
  96. cs->get_rx = mpc8xxx_spi_rx_buf_u16;
  97. cs->get_tx = mpc8xxx_spi_tx_buf_u16;
  98. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  99. cs->rx_shift = 16;
  100. cs->tx_shift = 16;
  101. }
  102. } else if (bits_per_word <= 32) {
  103. cs->get_rx = mpc8xxx_spi_rx_buf_u32;
  104. cs->get_tx = mpc8xxx_spi_tx_buf_u32;
  105. } else
  106. return -EINVAL;
  107. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE &&
  108. spi->mode & SPI_LSB_FIRST) {
  109. cs->tx_shift = 0;
  110. if (bits_per_word <= 8)
  111. cs->rx_shift = 8;
  112. else
  113. cs->rx_shift = 0;
  114. }
  115. mpc8xxx_spi->rx_shift = cs->rx_shift;
  116. mpc8xxx_spi->tx_shift = cs->tx_shift;
  117. mpc8xxx_spi->get_rx = cs->get_rx;
  118. mpc8xxx_spi->get_tx = cs->get_tx;
  119. return bits_per_word;
  120. }
  121. static int mspi_apply_qe_mode_quirks(struct spi_mpc8xxx_cs *cs,
  122. struct spi_device *spi,
  123. int bits_per_word)
  124. {
  125. /* QE uses Little Endian for words > 8
  126. * so transform all words > 8 into 8 bits
  127. * Unfortnatly that doesn't work for LSB so
  128. * reject these for now */
  129. /* Note: 32 bits word, LSB works iff
  130. * tfcr/rfcr is set to CPMFCR_GBL */
  131. if (spi->mode & SPI_LSB_FIRST &&
  132. bits_per_word > 8)
  133. return -EINVAL;
  134. if (bits_per_word > 8)
  135. return 8; /* pretend its 8 bits */
  136. return bits_per_word;
  137. }
  138. static int fsl_spi_setup_transfer(struct spi_device *spi,
  139. struct spi_transfer *t)
  140. {
  141. struct mpc8xxx_spi *mpc8xxx_spi;
  142. int bits_per_word = 0;
  143. u8 pm;
  144. u32 hz = 0;
  145. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  146. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  147. if (t) {
  148. bits_per_word = t->bits_per_word;
  149. hz = t->speed_hz;
  150. }
  151. /* spi_transfer level calls that work per-word */
  152. if (!bits_per_word)
  153. bits_per_word = spi->bits_per_word;
  154. /* Make sure its a bit width we support [4..16, 32] */
  155. if ((bits_per_word < 4)
  156. || ((bits_per_word > 16) && (bits_per_word != 32)))
  157. return -EINVAL;
  158. if (!hz)
  159. hz = spi->max_speed_hz;
  160. if (!(mpc8xxx_spi->flags & SPI_CPM_MODE))
  161. bits_per_word = mspi_apply_cpu_mode_quirks(cs, spi,
  162. mpc8xxx_spi,
  163. bits_per_word);
  164. else if (mpc8xxx_spi->flags & SPI_QE)
  165. bits_per_word = mspi_apply_qe_mode_quirks(cs, spi,
  166. bits_per_word);
  167. if (bits_per_word < 0)
  168. return bits_per_word;
  169. if (bits_per_word == 32)
  170. bits_per_word = 0;
  171. else
  172. bits_per_word = bits_per_word - 1;
  173. /* mask out bits we are going to set */
  174. cs->hw_mode &= ~(SPMODE_LEN(0xF) | SPMODE_DIV16
  175. | SPMODE_PM(0xF));
  176. cs->hw_mode |= SPMODE_LEN(bits_per_word);
  177. if ((mpc8xxx_spi->spibrg / hz) > 64) {
  178. cs->hw_mode |= SPMODE_DIV16;
  179. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 64) + 1;
  180. WARN_ONCE(pm > 16, "%s: Requested speed is too low: %d Hz. "
  181. "Will use %d Hz instead.\n", dev_name(&spi->dev),
  182. hz, mpc8xxx_spi->spibrg / 1024);
  183. if (pm > 16)
  184. pm = 16;
  185. } else {
  186. pm = (mpc8xxx_spi->spibrg - 1) / (hz * 4) + 1;
  187. }
  188. if (pm)
  189. pm--;
  190. cs->hw_mode |= SPMODE_PM(pm);
  191. fsl_spi_change_mode(spi);
  192. return 0;
  193. }
  194. static int fsl_spi_cpu_bufs(struct mpc8xxx_spi *mspi,
  195. struct spi_transfer *t, unsigned int len)
  196. {
  197. u32 word;
  198. struct fsl_spi_reg *reg_base = mspi->reg_base;
  199. mspi->count = len;
  200. /* enable rx ints */
  201. mpc8xxx_spi_write_reg(&reg_base->mask, SPIM_NE);
  202. /* transmit word */
  203. word = mspi->get_tx(mspi);
  204. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  205. return 0;
  206. }
  207. static int fsl_spi_bufs(struct spi_device *spi, struct spi_transfer *t,
  208. bool is_dma_mapped)
  209. {
  210. struct mpc8xxx_spi *mpc8xxx_spi = spi_master_get_devdata(spi->master);
  211. struct fsl_spi_reg *reg_base;
  212. unsigned int len = t->len;
  213. u8 bits_per_word;
  214. int ret;
  215. reg_base = mpc8xxx_spi->reg_base;
  216. bits_per_word = spi->bits_per_word;
  217. if (t->bits_per_word)
  218. bits_per_word = t->bits_per_word;
  219. if (bits_per_word > 8) {
  220. /* invalid length? */
  221. if (len & 1)
  222. return -EINVAL;
  223. len /= 2;
  224. }
  225. if (bits_per_word > 16) {
  226. /* invalid length? */
  227. if (len & 1)
  228. return -EINVAL;
  229. len /= 2;
  230. }
  231. mpc8xxx_spi->tx = t->tx_buf;
  232. mpc8xxx_spi->rx = t->rx_buf;
  233. INIT_COMPLETION(mpc8xxx_spi->done);
  234. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  235. ret = fsl_spi_cpm_bufs(mpc8xxx_spi, t, is_dma_mapped);
  236. else
  237. ret = fsl_spi_cpu_bufs(mpc8xxx_spi, t, len);
  238. if (ret)
  239. return ret;
  240. wait_for_completion(&mpc8xxx_spi->done);
  241. /* disable rx ints */
  242. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  243. if (mpc8xxx_spi->flags & SPI_CPM_MODE)
  244. fsl_spi_cpm_bufs_complete(mpc8xxx_spi);
  245. return mpc8xxx_spi->count;
  246. }
  247. static void fsl_spi_do_one_msg(struct spi_message *m)
  248. {
  249. struct spi_device *spi = m->spi;
  250. struct spi_transfer *t;
  251. unsigned int cs_change;
  252. const int nsecs = 50;
  253. int status;
  254. cs_change = 1;
  255. status = 0;
  256. list_for_each_entry(t, &m->transfers, transfer_list) {
  257. if (t->bits_per_word || t->speed_hz) {
  258. /* Don't allow changes if CS is active */
  259. status = -EINVAL;
  260. if (cs_change)
  261. status = fsl_spi_setup_transfer(spi, t);
  262. if (status < 0)
  263. break;
  264. }
  265. if (cs_change) {
  266. fsl_spi_chipselect(spi, BITBANG_CS_ACTIVE);
  267. ndelay(nsecs);
  268. }
  269. cs_change = t->cs_change;
  270. if (t->len)
  271. status = fsl_spi_bufs(spi, t, m->is_dma_mapped);
  272. if (status) {
  273. status = -EMSGSIZE;
  274. break;
  275. }
  276. m->actual_length += t->len;
  277. if (t->delay_usecs)
  278. udelay(t->delay_usecs);
  279. if (cs_change) {
  280. ndelay(nsecs);
  281. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  282. ndelay(nsecs);
  283. }
  284. }
  285. m->status = status;
  286. m->complete(m->context);
  287. if (status || !cs_change) {
  288. ndelay(nsecs);
  289. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  290. }
  291. fsl_spi_setup_transfer(spi, NULL);
  292. }
  293. static int fsl_spi_setup(struct spi_device *spi)
  294. {
  295. struct mpc8xxx_spi *mpc8xxx_spi;
  296. struct fsl_spi_reg *reg_base;
  297. int retval;
  298. u32 hw_mode;
  299. struct spi_mpc8xxx_cs *cs = spi->controller_state;
  300. if (!spi->max_speed_hz)
  301. return -EINVAL;
  302. if (!cs) {
  303. cs = kzalloc(sizeof *cs, GFP_KERNEL);
  304. if (!cs)
  305. return -ENOMEM;
  306. spi->controller_state = cs;
  307. }
  308. mpc8xxx_spi = spi_master_get_devdata(spi->master);
  309. reg_base = mpc8xxx_spi->reg_base;
  310. hw_mode = cs->hw_mode; /* Save original settings */
  311. cs->hw_mode = mpc8xxx_spi_read_reg(&reg_base->mode);
  312. /* mask out bits we are going to set */
  313. cs->hw_mode &= ~(SPMODE_CP_BEGIN_EDGECLK | SPMODE_CI_INACTIVEHIGH
  314. | SPMODE_REV | SPMODE_LOOP);
  315. if (spi->mode & SPI_CPHA)
  316. cs->hw_mode |= SPMODE_CP_BEGIN_EDGECLK;
  317. if (spi->mode & SPI_CPOL)
  318. cs->hw_mode |= SPMODE_CI_INACTIVEHIGH;
  319. if (!(spi->mode & SPI_LSB_FIRST))
  320. cs->hw_mode |= SPMODE_REV;
  321. if (spi->mode & SPI_LOOP)
  322. cs->hw_mode |= SPMODE_LOOP;
  323. retval = fsl_spi_setup_transfer(spi, NULL);
  324. if (retval < 0) {
  325. cs->hw_mode = hw_mode; /* Restore settings */
  326. return retval;
  327. }
  328. /* Initialize chipselect - might be active for SPI_CS_HIGH mode */
  329. fsl_spi_chipselect(spi, BITBANG_CS_INACTIVE);
  330. return 0;
  331. }
  332. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  333. {
  334. struct fsl_spi_reg *reg_base = mspi->reg_base;
  335. /* We need handle RX first */
  336. if (events & SPIE_NE) {
  337. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  338. if (mspi->rx)
  339. mspi->get_rx(rx_data, mspi);
  340. }
  341. if ((events & SPIE_NF) == 0)
  342. /* spin until TX is done */
  343. while (((events =
  344. mpc8xxx_spi_read_reg(&reg_base->event)) &
  345. SPIE_NF) == 0)
  346. cpu_relax();
  347. /* Clear the events */
  348. mpc8xxx_spi_write_reg(&reg_base->event, events);
  349. mspi->count -= 1;
  350. if (mspi->count) {
  351. u32 word = mspi->get_tx(mspi);
  352. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  353. } else {
  354. complete(&mspi->done);
  355. }
  356. }
  357. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  358. {
  359. struct mpc8xxx_spi *mspi = context_data;
  360. irqreturn_t ret = IRQ_NONE;
  361. u32 events;
  362. struct fsl_spi_reg *reg_base = mspi->reg_base;
  363. /* Get interrupt events(tx/rx) */
  364. events = mpc8xxx_spi_read_reg(&reg_base->event);
  365. if (events)
  366. ret = IRQ_HANDLED;
  367. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  368. if (mspi->flags & SPI_CPM_MODE)
  369. fsl_spi_cpm_irq(mspi, events);
  370. else
  371. fsl_spi_cpu_irq(mspi, events);
  372. return ret;
  373. }
  374. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  375. {
  376. iounmap(mspi->reg_base);
  377. fsl_spi_cpm_free(mspi);
  378. }
  379. static struct spi_master * fsl_spi_probe(struct device *dev,
  380. struct resource *mem, unsigned int irq)
  381. {
  382. struct fsl_spi_platform_data *pdata = dev->platform_data;
  383. struct spi_master *master;
  384. struct mpc8xxx_spi *mpc8xxx_spi;
  385. struct fsl_spi_reg *reg_base;
  386. u32 regval;
  387. int ret = 0;
  388. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  389. if (master == NULL) {
  390. ret = -ENOMEM;
  391. goto err;
  392. }
  393. dev_set_drvdata(dev, master);
  394. ret = mpc8xxx_spi_probe(dev, mem, irq);
  395. if (ret)
  396. goto err_probe;
  397. master->setup = fsl_spi_setup;
  398. mpc8xxx_spi = spi_master_get_devdata(master);
  399. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  400. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  401. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  402. if (ret)
  403. goto err_cpm_init;
  404. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  405. mpc8xxx_spi->rx_shift = 16;
  406. mpc8xxx_spi->tx_shift = 24;
  407. }
  408. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  409. if (mpc8xxx_spi->reg_base == NULL) {
  410. ret = -ENOMEM;
  411. goto err_ioremap;
  412. }
  413. /* Register for SPI Interrupt */
  414. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  415. 0, "fsl_spi", mpc8xxx_spi);
  416. if (ret != 0)
  417. goto free_irq;
  418. reg_base = mpc8xxx_spi->reg_base;
  419. /* SPI controller initializations */
  420. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  421. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  422. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  423. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  424. /* Enable SPI interface */
  425. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  426. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  427. regval |= SPMODE_OP;
  428. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  429. ret = spi_register_master(master);
  430. if (ret < 0)
  431. goto unreg_master;
  432. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  433. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  434. return master;
  435. unreg_master:
  436. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  437. free_irq:
  438. iounmap(mpc8xxx_spi->reg_base);
  439. err_ioremap:
  440. fsl_spi_cpm_free(mpc8xxx_spi);
  441. err_cpm_init:
  442. err_probe:
  443. spi_master_put(master);
  444. err:
  445. return ERR_PTR(ret);
  446. }
  447. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  448. {
  449. struct device *dev = spi->dev.parent->parent;
  450. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  451. u16 cs = spi->chip_select;
  452. int gpio = pinfo->gpios[cs];
  453. bool alow = pinfo->alow_flags[cs];
  454. gpio_set_value(gpio, on ^ alow);
  455. }
  456. static int of_fsl_spi_get_chipselects(struct device *dev)
  457. {
  458. struct device_node *np = dev->of_node;
  459. struct fsl_spi_platform_data *pdata = dev->platform_data;
  460. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  461. int ngpios;
  462. int i = 0;
  463. int ret;
  464. ngpios = of_gpio_count(np);
  465. if (ngpios <= 0) {
  466. /*
  467. * SPI w/o chip-select line. One SPI device is still permitted
  468. * though.
  469. */
  470. pdata->max_chipselect = 1;
  471. return 0;
  472. }
  473. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  474. if (!pinfo->gpios)
  475. return -ENOMEM;
  476. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  477. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  478. GFP_KERNEL);
  479. if (!pinfo->alow_flags) {
  480. ret = -ENOMEM;
  481. goto err_alloc_flags;
  482. }
  483. for (; i < ngpios; i++) {
  484. int gpio;
  485. enum of_gpio_flags flags;
  486. gpio = of_get_gpio_flags(np, i, &flags);
  487. if (!gpio_is_valid(gpio)) {
  488. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  489. ret = gpio;
  490. goto err_loop;
  491. }
  492. ret = gpio_request(gpio, dev_name(dev));
  493. if (ret) {
  494. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  495. goto err_loop;
  496. }
  497. pinfo->gpios[i] = gpio;
  498. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  499. ret = gpio_direction_output(pinfo->gpios[i],
  500. pinfo->alow_flags[i]);
  501. if (ret) {
  502. dev_err(dev, "can't set output direction for gpio "
  503. "#%d: %d\n", i, ret);
  504. goto err_loop;
  505. }
  506. }
  507. pdata->max_chipselect = ngpios;
  508. pdata->cs_control = fsl_spi_cs_control;
  509. return 0;
  510. err_loop:
  511. while (i >= 0) {
  512. if (gpio_is_valid(pinfo->gpios[i]))
  513. gpio_free(pinfo->gpios[i]);
  514. i--;
  515. }
  516. kfree(pinfo->alow_flags);
  517. pinfo->alow_flags = NULL;
  518. err_alloc_flags:
  519. kfree(pinfo->gpios);
  520. pinfo->gpios = NULL;
  521. return ret;
  522. }
  523. static int of_fsl_spi_free_chipselects(struct device *dev)
  524. {
  525. struct fsl_spi_platform_data *pdata = dev->platform_data;
  526. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  527. int i;
  528. if (!pinfo->gpios)
  529. return 0;
  530. for (i = 0; i < pdata->max_chipselect; i++) {
  531. if (gpio_is_valid(pinfo->gpios[i]))
  532. gpio_free(pinfo->gpios[i]);
  533. }
  534. kfree(pinfo->gpios);
  535. kfree(pinfo->alow_flags);
  536. return 0;
  537. }
  538. static int of_fsl_spi_probe(struct platform_device *ofdev)
  539. {
  540. struct device *dev = &ofdev->dev;
  541. struct device_node *np = ofdev->dev.of_node;
  542. struct spi_master *master;
  543. struct resource mem;
  544. int irq;
  545. int ret = -ENOMEM;
  546. ret = of_mpc8xxx_spi_probe(ofdev);
  547. if (ret)
  548. return ret;
  549. ret = of_fsl_spi_get_chipselects(dev);
  550. if (ret)
  551. goto err;
  552. ret = of_address_to_resource(np, 0, &mem);
  553. if (ret)
  554. goto err;
  555. irq = irq_of_parse_and_map(np, 0);
  556. if (!irq) {
  557. ret = -EINVAL;
  558. goto err;
  559. }
  560. master = fsl_spi_probe(dev, &mem, irq);
  561. if (IS_ERR(master)) {
  562. ret = PTR_ERR(master);
  563. goto err;
  564. }
  565. return 0;
  566. err:
  567. of_fsl_spi_free_chipselects(dev);
  568. return ret;
  569. }
  570. static int of_fsl_spi_remove(struct platform_device *ofdev)
  571. {
  572. int ret;
  573. ret = mpc8xxx_spi_remove(&ofdev->dev);
  574. if (ret)
  575. return ret;
  576. of_fsl_spi_free_chipselects(&ofdev->dev);
  577. return 0;
  578. }
  579. static const struct of_device_id of_fsl_spi_match[] = {
  580. { .compatible = "fsl,spi" },
  581. {}
  582. };
  583. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  584. static struct platform_driver of_fsl_spi_driver = {
  585. .driver = {
  586. .name = "fsl_spi",
  587. .owner = THIS_MODULE,
  588. .of_match_table = of_fsl_spi_match,
  589. },
  590. .probe = of_fsl_spi_probe,
  591. .remove = of_fsl_spi_remove,
  592. };
  593. #ifdef CONFIG_MPC832x_RDB
  594. /*
  595. * XXX XXX XXX
  596. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  597. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  598. * tree can work with OpenFirmware driver. But for now we support old trees
  599. * as well.
  600. */
  601. static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  602. {
  603. struct resource *mem;
  604. int irq;
  605. struct spi_master *master;
  606. if (!pdev->dev.platform_data)
  607. return -EINVAL;
  608. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  609. if (!mem)
  610. return -EINVAL;
  611. irq = platform_get_irq(pdev, 0);
  612. if (irq <= 0)
  613. return -EINVAL;
  614. master = fsl_spi_probe(&pdev->dev, mem, irq);
  615. return PTR_RET(master);
  616. }
  617. static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  618. {
  619. return mpc8xxx_spi_remove(&pdev->dev);
  620. }
  621. MODULE_ALIAS("platform:mpc8xxx_spi");
  622. static struct platform_driver mpc8xxx_spi_driver = {
  623. .probe = plat_mpc8xxx_spi_probe,
  624. .remove = plat_mpc8xxx_spi_remove,
  625. .driver = {
  626. .name = "mpc8xxx_spi",
  627. .owner = THIS_MODULE,
  628. },
  629. };
  630. static bool legacy_driver_failed;
  631. static void __init legacy_driver_register(void)
  632. {
  633. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  634. }
  635. static void __exit legacy_driver_unregister(void)
  636. {
  637. if (legacy_driver_failed)
  638. return;
  639. platform_driver_unregister(&mpc8xxx_spi_driver);
  640. }
  641. #else
  642. static void __init legacy_driver_register(void) {}
  643. static void __exit legacy_driver_unregister(void) {}
  644. #endif /* CONFIG_MPC832x_RDB */
  645. static int __init fsl_spi_init(void)
  646. {
  647. legacy_driver_register();
  648. return platform_driver_register(&of_fsl_spi_driver);
  649. }
  650. module_init(fsl_spi_init);
  651. static void __exit fsl_spi_exit(void)
  652. {
  653. platform_driver_unregister(&of_fsl_spi_driver);
  654. legacy_driver_unregister();
  655. }
  656. module_exit(fsl_spi_exit);
  657. MODULE_AUTHOR("Kumar Gala");
  658. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  659. MODULE_LICENSE("GPL");