spi-fsl-spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. return 0;
  329. }
  330. static void fsl_spi_cpu_irq(struct mpc8xxx_spi *mspi, u32 events)
  331. {
  332. struct fsl_spi_reg *reg_base = mspi->reg_base;
  333. /* We need handle RX first */
  334. if (events & SPIE_NE) {
  335. u32 rx_data = mpc8xxx_spi_read_reg(&reg_base->receive);
  336. if (mspi->rx)
  337. mspi->get_rx(rx_data, mspi);
  338. }
  339. if ((events & SPIE_NF) == 0)
  340. /* spin until TX is done */
  341. while (((events =
  342. mpc8xxx_spi_read_reg(&reg_base->event)) &
  343. SPIE_NF) == 0)
  344. cpu_relax();
  345. /* Clear the events */
  346. mpc8xxx_spi_write_reg(&reg_base->event, events);
  347. mspi->count -= 1;
  348. if (mspi->count) {
  349. u32 word = mspi->get_tx(mspi);
  350. mpc8xxx_spi_write_reg(&reg_base->transmit, word);
  351. } else {
  352. complete(&mspi->done);
  353. }
  354. }
  355. static irqreturn_t fsl_spi_irq(s32 irq, void *context_data)
  356. {
  357. struct mpc8xxx_spi *mspi = context_data;
  358. irqreturn_t ret = IRQ_NONE;
  359. u32 events;
  360. struct fsl_spi_reg *reg_base = mspi->reg_base;
  361. /* Get interrupt events(tx/rx) */
  362. events = mpc8xxx_spi_read_reg(&reg_base->event);
  363. if (events)
  364. ret = IRQ_HANDLED;
  365. dev_dbg(mspi->dev, "%s: events %x\n", __func__, events);
  366. if (mspi->flags & SPI_CPM_MODE)
  367. fsl_spi_cpm_irq(mspi, events);
  368. else
  369. fsl_spi_cpu_irq(mspi, events);
  370. return ret;
  371. }
  372. static void fsl_spi_remove(struct mpc8xxx_spi *mspi)
  373. {
  374. iounmap(mspi->reg_base);
  375. fsl_spi_cpm_free(mspi);
  376. }
  377. static struct spi_master * fsl_spi_probe(struct device *dev,
  378. struct resource *mem, unsigned int irq)
  379. {
  380. struct fsl_spi_platform_data *pdata = dev->platform_data;
  381. struct spi_master *master;
  382. struct mpc8xxx_spi *mpc8xxx_spi;
  383. struct fsl_spi_reg *reg_base;
  384. u32 regval;
  385. int ret = 0;
  386. master = spi_alloc_master(dev, sizeof(struct mpc8xxx_spi));
  387. if (master == NULL) {
  388. ret = -ENOMEM;
  389. goto err;
  390. }
  391. dev_set_drvdata(dev, master);
  392. ret = mpc8xxx_spi_probe(dev, mem, irq);
  393. if (ret)
  394. goto err_probe;
  395. master->setup = fsl_spi_setup;
  396. mpc8xxx_spi = spi_master_get_devdata(master);
  397. mpc8xxx_spi->spi_do_one_msg = fsl_spi_do_one_msg;
  398. mpc8xxx_spi->spi_remove = fsl_spi_remove;
  399. ret = fsl_spi_cpm_init(mpc8xxx_spi);
  400. if (ret)
  401. goto err_cpm_init;
  402. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE) {
  403. mpc8xxx_spi->rx_shift = 16;
  404. mpc8xxx_spi->tx_shift = 24;
  405. }
  406. mpc8xxx_spi->reg_base = ioremap(mem->start, resource_size(mem));
  407. if (mpc8xxx_spi->reg_base == NULL) {
  408. ret = -ENOMEM;
  409. goto err_ioremap;
  410. }
  411. /* Register for SPI Interrupt */
  412. ret = request_irq(mpc8xxx_spi->irq, fsl_spi_irq,
  413. 0, "fsl_spi", mpc8xxx_spi);
  414. if (ret != 0)
  415. goto free_irq;
  416. reg_base = mpc8xxx_spi->reg_base;
  417. /* SPI controller initializations */
  418. mpc8xxx_spi_write_reg(&reg_base->mode, 0);
  419. mpc8xxx_spi_write_reg(&reg_base->mask, 0);
  420. mpc8xxx_spi_write_reg(&reg_base->command, 0);
  421. mpc8xxx_spi_write_reg(&reg_base->event, 0xffffffff);
  422. /* Enable SPI interface */
  423. regval = pdata->initial_spmode | SPMODE_INIT_VAL | SPMODE_ENABLE;
  424. if (mpc8xxx_spi->flags & SPI_QE_CPU_MODE)
  425. regval |= SPMODE_OP;
  426. mpc8xxx_spi_write_reg(&reg_base->mode, regval);
  427. ret = spi_register_master(master);
  428. if (ret < 0)
  429. goto unreg_master;
  430. dev_info(dev, "at 0x%p (irq = %d), %s mode\n", reg_base,
  431. mpc8xxx_spi->irq, mpc8xxx_spi_strmode(mpc8xxx_spi->flags));
  432. return master;
  433. unreg_master:
  434. free_irq(mpc8xxx_spi->irq, mpc8xxx_spi);
  435. free_irq:
  436. iounmap(mpc8xxx_spi->reg_base);
  437. err_ioremap:
  438. fsl_spi_cpm_free(mpc8xxx_spi);
  439. err_cpm_init:
  440. err_probe:
  441. spi_master_put(master);
  442. err:
  443. return ERR_PTR(ret);
  444. }
  445. static void fsl_spi_cs_control(struct spi_device *spi, bool on)
  446. {
  447. struct device *dev = spi->dev.parent->parent;
  448. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(dev->platform_data);
  449. u16 cs = spi->chip_select;
  450. int gpio = pinfo->gpios[cs];
  451. bool alow = pinfo->alow_flags[cs];
  452. gpio_set_value(gpio, on ^ alow);
  453. }
  454. static int of_fsl_spi_get_chipselects(struct device *dev)
  455. {
  456. struct device_node *np = dev->of_node;
  457. struct fsl_spi_platform_data *pdata = dev->platform_data;
  458. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  459. int ngpios;
  460. int i = 0;
  461. int ret;
  462. ngpios = of_gpio_count(np);
  463. if (ngpios <= 0) {
  464. /*
  465. * SPI w/o chip-select line. One SPI device is still permitted
  466. * though.
  467. */
  468. pdata->max_chipselect = 1;
  469. return 0;
  470. }
  471. pinfo->gpios = kmalloc(ngpios * sizeof(*pinfo->gpios), GFP_KERNEL);
  472. if (!pinfo->gpios)
  473. return -ENOMEM;
  474. memset(pinfo->gpios, -1, ngpios * sizeof(*pinfo->gpios));
  475. pinfo->alow_flags = kzalloc(ngpios * sizeof(*pinfo->alow_flags),
  476. GFP_KERNEL);
  477. if (!pinfo->alow_flags) {
  478. ret = -ENOMEM;
  479. goto err_alloc_flags;
  480. }
  481. for (; i < ngpios; i++) {
  482. int gpio;
  483. enum of_gpio_flags flags;
  484. gpio = of_get_gpio_flags(np, i, &flags);
  485. if (!gpio_is_valid(gpio)) {
  486. dev_err(dev, "invalid gpio #%d: %d\n", i, gpio);
  487. ret = gpio;
  488. goto err_loop;
  489. }
  490. ret = gpio_request(gpio, dev_name(dev));
  491. if (ret) {
  492. dev_err(dev, "can't request gpio #%d: %d\n", i, ret);
  493. goto err_loop;
  494. }
  495. pinfo->gpios[i] = gpio;
  496. pinfo->alow_flags[i] = flags & OF_GPIO_ACTIVE_LOW;
  497. ret = gpio_direction_output(pinfo->gpios[i],
  498. pinfo->alow_flags[i]);
  499. if (ret) {
  500. dev_err(dev, "can't set output direction for gpio "
  501. "#%d: %d\n", i, ret);
  502. goto err_loop;
  503. }
  504. }
  505. pdata->max_chipselect = ngpios;
  506. pdata->cs_control = fsl_spi_cs_control;
  507. return 0;
  508. err_loop:
  509. while (i >= 0) {
  510. if (gpio_is_valid(pinfo->gpios[i]))
  511. gpio_free(pinfo->gpios[i]);
  512. i--;
  513. }
  514. kfree(pinfo->alow_flags);
  515. pinfo->alow_flags = NULL;
  516. err_alloc_flags:
  517. kfree(pinfo->gpios);
  518. pinfo->gpios = NULL;
  519. return ret;
  520. }
  521. static int of_fsl_spi_free_chipselects(struct device *dev)
  522. {
  523. struct fsl_spi_platform_data *pdata = dev->platform_data;
  524. struct mpc8xxx_spi_probe_info *pinfo = to_of_pinfo(pdata);
  525. int i;
  526. if (!pinfo->gpios)
  527. return 0;
  528. for (i = 0; i < pdata->max_chipselect; i++) {
  529. if (gpio_is_valid(pinfo->gpios[i]))
  530. gpio_free(pinfo->gpios[i]);
  531. }
  532. kfree(pinfo->gpios);
  533. kfree(pinfo->alow_flags);
  534. return 0;
  535. }
  536. static int of_fsl_spi_probe(struct platform_device *ofdev)
  537. {
  538. struct device *dev = &ofdev->dev;
  539. struct device_node *np = ofdev->dev.of_node;
  540. struct spi_master *master;
  541. struct resource mem;
  542. int irq;
  543. int ret = -ENOMEM;
  544. ret = of_mpc8xxx_spi_probe(ofdev);
  545. if (ret)
  546. return ret;
  547. ret = of_fsl_spi_get_chipselects(dev);
  548. if (ret)
  549. goto err;
  550. ret = of_address_to_resource(np, 0, &mem);
  551. if (ret)
  552. goto err;
  553. irq = irq_of_parse_and_map(np, 0);
  554. if (!irq) {
  555. ret = -EINVAL;
  556. goto err;
  557. }
  558. master = fsl_spi_probe(dev, &mem, irq);
  559. if (IS_ERR(master)) {
  560. ret = PTR_ERR(master);
  561. goto err;
  562. }
  563. return 0;
  564. err:
  565. of_fsl_spi_free_chipselects(dev);
  566. return ret;
  567. }
  568. static int of_fsl_spi_remove(struct platform_device *ofdev)
  569. {
  570. int ret;
  571. ret = mpc8xxx_spi_remove(&ofdev->dev);
  572. if (ret)
  573. return ret;
  574. of_fsl_spi_free_chipselects(&ofdev->dev);
  575. return 0;
  576. }
  577. static const struct of_device_id of_fsl_spi_match[] = {
  578. { .compatible = "fsl,spi" },
  579. {}
  580. };
  581. MODULE_DEVICE_TABLE(of, of_fsl_spi_match);
  582. static struct platform_driver of_fsl_spi_driver = {
  583. .driver = {
  584. .name = "fsl_spi",
  585. .owner = THIS_MODULE,
  586. .of_match_table = of_fsl_spi_match,
  587. },
  588. .probe = of_fsl_spi_probe,
  589. .remove = of_fsl_spi_remove,
  590. };
  591. #ifdef CONFIG_MPC832x_RDB
  592. /*
  593. * XXX XXX XXX
  594. * This is "legacy" platform driver, was used by the MPC8323E-RDB boards
  595. * only. The driver should go away soon, since newer MPC8323E-RDB's device
  596. * tree can work with OpenFirmware driver. But for now we support old trees
  597. * as well.
  598. */
  599. static int plat_mpc8xxx_spi_probe(struct platform_device *pdev)
  600. {
  601. struct resource *mem;
  602. int irq;
  603. struct spi_master *master;
  604. if (!pdev->dev.platform_data)
  605. return -EINVAL;
  606. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  607. if (!mem)
  608. return -EINVAL;
  609. irq = platform_get_irq(pdev, 0);
  610. if (irq <= 0)
  611. return -EINVAL;
  612. master = fsl_spi_probe(&pdev->dev, mem, irq);
  613. return PTR_RET(master);
  614. }
  615. static int plat_mpc8xxx_spi_remove(struct platform_device *pdev)
  616. {
  617. return mpc8xxx_spi_remove(&pdev->dev);
  618. }
  619. MODULE_ALIAS("platform:mpc8xxx_spi");
  620. static struct platform_driver mpc8xxx_spi_driver = {
  621. .probe = plat_mpc8xxx_spi_probe,
  622. .remove = plat_mpc8xxx_spi_remove,
  623. .driver = {
  624. .name = "mpc8xxx_spi",
  625. .owner = THIS_MODULE,
  626. },
  627. };
  628. static bool legacy_driver_failed;
  629. static void __init legacy_driver_register(void)
  630. {
  631. legacy_driver_failed = platform_driver_register(&mpc8xxx_spi_driver);
  632. }
  633. static void __exit legacy_driver_unregister(void)
  634. {
  635. if (legacy_driver_failed)
  636. return;
  637. platform_driver_unregister(&mpc8xxx_spi_driver);
  638. }
  639. #else
  640. static void __init legacy_driver_register(void) {}
  641. static void __exit legacy_driver_unregister(void) {}
  642. #endif /* CONFIG_MPC832x_RDB */
  643. static int __init fsl_spi_init(void)
  644. {
  645. legacy_driver_register();
  646. return platform_driver_register(&of_fsl_spi_driver);
  647. }
  648. module_init(fsl_spi_init);
  649. static void __exit fsl_spi_exit(void)
  650. {
  651. platform_driver_unregister(&of_fsl_spi_driver);
  652. legacy_driver_unregister();
  653. }
  654. module_exit(fsl_spi_exit);
  655. MODULE_AUTHOR("Kumar Gala");
  656. MODULE_DESCRIPTION("Simple Freescale SPI Driver");
  657. MODULE_LICENSE("GPL");