spi-fsl-spi.c 18 KB

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