spi_topcliff_pch.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304
  1. /*
  2. * SPI bus driver for the Topcliff PCH used by Intel SoCs
  3. *
  4. * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/pci.h>
  21. #include <linux/wait.h>
  22. #include <linux/spi/spi.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/spi/spidev.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. /* Register offsets */
  29. #define PCH_SPCR 0x00 /* SPI control register */
  30. #define PCH_SPBRR 0x04 /* SPI baud rate register */
  31. #define PCH_SPSR 0x08 /* SPI status register */
  32. #define PCH_SPDWR 0x0C /* SPI write data register */
  33. #define PCH_SPDRR 0x10 /* SPI read data register */
  34. #define PCH_SSNXCR 0x18 /* SSN Expand Control Register */
  35. #define PCH_SRST 0x1C /* SPI reset register */
  36. #define PCH_SPSR_TFD 0x000007C0
  37. #define PCH_SPSR_RFD 0x0000F800
  38. #define PCH_READABLE(x) (((x) & PCH_SPSR_RFD)>>11)
  39. #define PCH_WRITABLE(x) (((x) & PCH_SPSR_TFD)>>6)
  40. #define PCH_RX_THOLD 7
  41. #define PCH_RX_THOLD_MAX 15
  42. #define PCH_MAX_BAUDRATE 5000000
  43. #define PCH_MAX_FIFO_DEPTH 16
  44. #define STATUS_RUNNING 1
  45. #define STATUS_EXITING 2
  46. #define PCH_SLEEP_TIME 10
  47. #define PCH_ADDRESS_SIZE 0x20
  48. #define SSN_LOW 0x02U
  49. #define SSN_NO_CONTROL 0x00U
  50. #define PCH_MAX_CS 0xFF
  51. #define PCI_DEVICE_ID_GE_SPI 0x8816
  52. #define SPCR_SPE_BIT (1 << 0)
  53. #define SPCR_MSTR_BIT (1 << 1)
  54. #define SPCR_LSBF_BIT (1 << 4)
  55. #define SPCR_CPHA_BIT (1 << 5)
  56. #define SPCR_CPOL_BIT (1 << 6)
  57. #define SPCR_TFIE_BIT (1 << 8)
  58. #define SPCR_RFIE_BIT (1 << 9)
  59. #define SPCR_FIE_BIT (1 << 10)
  60. #define SPCR_ORIE_BIT (1 << 11)
  61. #define SPCR_MDFIE_BIT (1 << 12)
  62. #define SPCR_FICLR_BIT (1 << 24)
  63. #define SPSR_TFI_BIT (1 << 0)
  64. #define SPSR_RFI_BIT (1 << 1)
  65. #define SPSR_FI_BIT (1 << 2)
  66. #define SPBRR_SIZE_BIT (1 << 10)
  67. #define PCH_ALL (SPCR_TFIE_BIT|SPCR_RFIE_BIT|SPCR_FIE_BIT|SPCR_ORIE_BIT|SPCR_MDFIE_BIT)
  68. #define SPCR_RFIC_FIELD 20
  69. #define SPCR_TFIC_FIELD 16
  70. #define SPSR_INT_BITS 0x1F
  71. #define MASK_SPBRR_SPBR_BITS (~((1 << 10) - 1))
  72. #define MASK_RFIC_SPCR_BITS (~(0xf << 20))
  73. #define MASK_TFIC_SPCR_BITS (~(0xf000f << 12))
  74. #define PCH_CLOCK_HZ 50000000
  75. #define PCH_MAX_SPBR 1023
  76. /**
  77. * struct pch_spi_data - Holds the SPI channel specific details
  78. * @io_remap_addr: The remapped PCI base address
  79. * @master: Pointer to the SPI master structure
  80. * @work: Reference to work queue handler
  81. * @wk: Workqueue for carrying out execution of the
  82. * requests
  83. * @wait: Wait queue for waking up upon receiving an
  84. * interrupt.
  85. * @transfer_complete: Status of SPI Transfer
  86. * @bcurrent_msg_processing: Status flag for message processing
  87. * @lock: Lock for protecting this structure
  88. * @queue: SPI Message queue
  89. * @status: Status of the SPI driver
  90. * @bpw_len: Length of data to be transferred in bits per
  91. * word
  92. * @transfer_active: Flag showing active transfer
  93. * @tx_index: Transmit data count; for bookkeeping during
  94. * transfer
  95. * @rx_index: Receive data count; for bookkeeping during
  96. * transfer
  97. * @tx_buff: Buffer for data to be transmitted
  98. * @rx_index: Buffer for Received data
  99. * @n_curnt_chip: The chip number that this SPI driver currently
  100. * operates on
  101. * @current_chip: Reference to the current chip that this SPI
  102. * driver currently operates on
  103. * @current_msg: The current message that this SPI driver is
  104. * handling
  105. * @cur_trans: The current transfer that this SPI driver is
  106. * handling
  107. * @board_dat: Reference to the SPI device data structure
  108. */
  109. struct pch_spi_data {
  110. void __iomem *io_remap_addr;
  111. struct spi_master *master;
  112. struct work_struct work;
  113. struct workqueue_struct *wk;
  114. wait_queue_head_t wait;
  115. u8 transfer_complete;
  116. u8 bcurrent_msg_processing;
  117. spinlock_t lock;
  118. struct list_head queue;
  119. u8 status;
  120. u32 bpw_len;
  121. u8 transfer_active;
  122. u32 tx_index;
  123. u32 rx_index;
  124. u16 *pkt_tx_buff;
  125. u16 *pkt_rx_buff;
  126. u8 n_curnt_chip;
  127. struct spi_device *current_chip;
  128. struct spi_message *current_msg;
  129. struct spi_transfer *cur_trans;
  130. struct pch_spi_board_data *board_dat;
  131. };
  132. /**
  133. * struct pch_spi_board_data - Holds the SPI device specific details
  134. * @pdev: Pointer to the PCI device
  135. * @irq_reg_sts: Status of IRQ registration
  136. * @pci_req_sts: Status of pci_request_regions
  137. * @suspend_sts: Status of suspend
  138. * @data: Pointer to SPI channel data structure
  139. */
  140. struct pch_spi_board_data {
  141. struct pci_dev *pdev;
  142. u8 irq_reg_sts;
  143. u8 pci_req_sts;
  144. u8 suspend_sts;
  145. struct pch_spi_data *data;
  146. };
  147. static struct pci_device_id pch_spi_pcidev_id[] = {
  148. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_GE_SPI)},
  149. {0,}
  150. };
  151. /**
  152. * pch_spi_writereg() - Performs register writes
  153. * @master: Pointer to struct spi_master.
  154. * @idx: Register offset.
  155. * @val: Value to be written to register.
  156. */
  157. static inline void pch_spi_writereg(struct spi_master *master, int idx, u32 val)
  158. {
  159. struct pch_spi_data *data = spi_master_get_devdata(master);
  160. iowrite32(val, (data->io_remap_addr + idx));
  161. }
  162. /**
  163. * pch_spi_readreg() - Performs register reads
  164. * @master: Pointer to struct spi_master.
  165. * @idx: Register offset.
  166. */
  167. static inline u32 pch_spi_readreg(struct spi_master *master, int idx)
  168. {
  169. struct pch_spi_data *data = spi_master_get_devdata(master);
  170. return ioread32(data->io_remap_addr + idx);
  171. }
  172. static inline void pch_spi_setclr_reg(struct spi_master *master, int idx,
  173. u32 set, u32 clr)
  174. {
  175. u32 tmp = pch_spi_readreg(master, idx);
  176. tmp = (tmp & ~clr) | set;
  177. pch_spi_writereg(master, idx, tmp);
  178. }
  179. static void pch_spi_set_master_mode(struct spi_master *master)
  180. {
  181. pch_spi_setclr_reg(master, PCH_SPCR, SPCR_MSTR_BIT, 0);
  182. }
  183. /**
  184. * pch_spi_clear_fifo() - Clears the Transmit and Receive FIFOs
  185. * @master: Pointer to struct spi_master.
  186. */
  187. static void pch_spi_clear_fifo(struct spi_master *master)
  188. {
  189. pch_spi_setclr_reg(master, PCH_SPCR, SPCR_FICLR_BIT, 0);
  190. pch_spi_setclr_reg(master, PCH_SPCR, 0, SPCR_FICLR_BIT);
  191. }
  192. static void pch_spi_handler_sub(struct pch_spi_data *data, u32 reg_spsr_val,
  193. void __iomem *io_remap_addr)
  194. {
  195. u32 n_read, tx_index, rx_index, bpw_len;
  196. u16 *pkt_rx_buffer, *pkt_tx_buff;
  197. int read_cnt;
  198. u32 reg_spcr_val;
  199. void __iomem *spsr;
  200. void __iomem *spdrr;
  201. void __iomem *spdwr;
  202. spsr = io_remap_addr + PCH_SPSR;
  203. iowrite32(reg_spsr_val, spsr);
  204. if (data->transfer_active) {
  205. rx_index = data->rx_index;
  206. tx_index = data->tx_index;
  207. bpw_len = data->bpw_len;
  208. pkt_rx_buffer = data->pkt_rx_buff;
  209. pkt_tx_buff = data->pkt_tx_buff;
  210. spdrr = io_remap_addr + PCH_SPDRR;
  211. spdwr = io_remap_addr + PCH_SPDWR;
  212. n_read = PCH_READABLE(reg_spsr_val);
  213. for (read_cnt = 0; (read_cnt < n_read); read_cnt++) {
  214. pkt_rx_buffer[rx_index++] = ioread32(spdrr);
  215. if (tx_index < bpw_len)
  216. iowrite32(pkt_tx_buff[tx_index++], spdwr);
  217. }
  218. /* disable RFI if not needed */
  219. if ((bpw_len - rx_index) <= PCH_MAX_FIFO_DEPTH) {
  220. reg_spcr_val = ioread32(io_remap_addr + PCH_SPCR);
  221. reg_spcr_val &= ~SPCR_RFIE_BIT; /* disable RFI */
  222. /* reset rx threshold */
  223. reg_spcr_val &= MASK_RFIC_SPCR_BITS;
  224. reg_spcr_val |= (PCH_RX_THOLD_MAX << SPCR_RFIC_FIELD);
  225. iowrite32(((reg_spcr_val) &= (~(SPCR_RFIE_BIT))),
  226. (io_remap_addr + PCH_SPCR));
  227. }
  228. /* update counts */
  229. data->tx_index = tx_index;
  230. data->rx_index = rx_index;
  231. }
  232. /* if transfer complete interrupt */
  233. if (reg_spsr_val & SPSR_FI_BIT) {
  234. /* disable FI & RFI interrupts */
  235. pch_spi_setclr_reg(data->master, PCH_SPCR, 0,
  236. SPCR_FIE_BIT | SPCR_RFIE_BIT);
  237. /* transfer is completed;inform pch_spi_process_messages */
  238. data->transfer_complete = true;
  239. wake_up(&data->wait);
  240. }
  241. }
  242. /**
  243. * pch_spi_handler() - Interrupt handler
  244. * @irq: The interrupt number.
  245. * @dev_id: Pointer to struct pch_spi_board_data.
  246. */
  247. static irqreturn_t pch_spi_handler(int irq, void *dev_id)
  248. {
  249. u32 reg_spsr_val;
  250. struct pch_spi_data *data;
  251. void __iomem *spsr;
  252. void __iomem *io_remap_addr;
  253. irqreturn_t ret = IRQ_NONE;
  254. struct pch_spi_board_data *board_dat = dev_id;
  255. if (board_dat->suspend_sts) {
  256. dev_dbg(&board_dat->pdev->dev,
  257. "%s returning due to suspend\n", __func__);
  258. return IRQ_NONE;
  259. }
  260. data = board_dat->data;
  261. io_remap_addr = data->io_remap_addr;
  262. spsr = io_remap_addr + PCH_SPSR;
  263. reg_spsr_val = ioread32(spsr);
  264. /* Check if the interrupt is for SPI device */
  265. if (reg_spsr_val & (SPSR_FI_BIT | SPSR_RFI_BIT)) {
  266. pch_spi_handler_sub(data, reg_spsr_val, io_remap_addr);
  267. ret = IRQ_HANDLED;
  268. }
  269. dev_dbg(&board_dat->pdev->dev, "%s EXIT return value=%d\n",
  270. __func__, ret);
  271. return ret;
  272. }
  273. /**
  274. * pch_spi_set_baud_rate() - Sets SPBR field in SPBRR
  275. * @master: Pointer to struct spi_master.
  276. * @speed_hz: Baud rate.
  277. */
  278. static void pch_spi_set_baud_rate(struct spi_master *master, u32 speed_hz)
  279. {
  280. u32 n_spbr = PCH_CLOCK_HZ / (speed_hz * 2);
  281. /* if baud rate is less than we can support limit it */
  282. if (n_spbr > PCH_MAX_SPBR)
  283. n_spbr = PCH_MAX_SPBR;
  284. pch_spi_setclr_reg(master, PCH_SPBRR, n_spbr, ~MASK_SPBRR_SPBR_BITS);
  285. }
  286. /**
  287. * pch_spi_set_bits_per_word() - Sets SIZE field in SPBRR
  288. * @master: Pointer to struct spi_master.
  289. * @bits_per_word: Bits per word for SPI transfer.
  290. */
  291. static void pch_spi_set_bits_per_word(struct spi_master *master,
  292. u8 bits_per_word)
  293. {
  294. if (bits_per_word == 8)
  295. pch_spi_setclr_reg(master, PCH_SPBRR, 0, SPBRR_SIZE_BIT);
  296. else
  297. pch_spi_setclr_reg(master, PCH_SPBRR, SPBRR_SIZE_BIT, 0);
  298. }
  299. /**
  300. * pch_spi_setup_transfer() - Configures the PCH SPI hardware for transfer
  301. * @spi: Pointer to struct spi_device.
  302. */
  303. static void pch_spi_setup_transfer(struct spi_device *spi)
  304. {
  305. u32 flags = 0;
  306. dev_dbg(&spi->dev, "%s SPBRR content =%x setting baud rate=%d\n",
  307. __func__, pch_spi_readreg(spi->master, PCH_SPBRR),
  308. spi->max_speed_hz);
  309. pch_spi_set_baud_rate(spi->master, spi->max_speed_hz);
  310. /* set bits per word */
  311. pch_spi_set_bits_per_word(spi->master, spi->bits_per_word);
  312. if (!(spi->mode & SPI_LSB_FIRST))
  313. flags |= SPCR_LSBF_BIT;
  314. if (spi->mode & SPI_CPOL)
  315. flags |= SPCR_CPOL_BIT;
  316. if (spi->mode & SPI_CPHA)
  317. flags |= SPCR_CPHA_BIT;
  318. pch_spi_setclr_reg(spi->master, PCH_SPCR, flags,
  319. (SPCR_LSBF_BIT | SPCR_CPOL_BIT | SPCR_CPHA_BIT));
  320. /* Clear the FIFO by toggling FICLR to 1 and back to 0 */
  321. pch_spi_clear_fifo(spi->master);
  322. }
  323. /**
  324. * pch_spi_reset() - Clears SPI registers
  325. * @master: Pointer to struct spi_master.
  326. */
  327. static void pch_spi_reset(struct spi_master *master)
  328. {
  329. /* write 1 to reset SPI */
  330. pch_spi_writereg(master, PCH_SRST, 0x1);
  331. /* clear reset */
  332. pch_spi_writereg(master, PCH_SRST, 0x0);
  333. }
  334. static int pch_spi_setup(struct spi_device *pspi)
  335. {
  336. /* check bits per word */
  337. if (pspi->bits_per_word == 0) {
  338. pspi->bits_per_word = 8;
  339. dev_dbg(&pspi->dev, "%s 8 bits per word\n", __func__);
  340. }
  341. if ((pspi->bits_per_word != 8) && (pspi->bits_per_word != 16)) {
  342. dev_err(&pspi->dev, "%s Invalid bits per word\n", __func__);
  343. return -EINVAL;
  344. }
  345. /* Check baud rate setting */
  346. /* if baud rate of chip is greater than
  347. max we can support,return error */
  348. if ((pspi->max_speed_hz) > PCH_MAX_BAUDRATE)
  349. pspi->max_speed_hz = PCH_MAX_BAUDRATE;
  350. dev_dbg(&pspi->dev, "%s MODE = %x\n", __func__,
  351. (pspi->mode) & (SPI_CPOL | SPI_CPHA));
  352. return 0;
  353. }
  354. static int pch_spi_transfer(struct spi_device *pspi, struct spi_message *pmsg)
  355. {
  356. struct spi_transfer *transfer;
  357. struct pch_spi_data *data = spi_master_get_devdata(pspi->master);
  358. int retval;
  359. unsigned long flags;
  360. /* validate spi message and baud rate */
  361. if (unlikely(list_empty(&pmsg->transfers) == 1)) {
  362. dev_err(&pspi->dev, "%s list empty\n", __func__);
  363. retval = -EINVAL;
  364. goto err_out;
  365. }
  366. if (unlikely(pspi->max_speed_hz == 0)) {
  367. dev_err(&pspi->dev, "%s pch_spi_tranfer maxspeed=%d\n",
  368. __func__, pspi->max_speed_hz);
  369. retval = -EINVAL;
  370. goto err_out;
  371. }
  372. dev_dbg(&pspi->dev, "%s Transfer List not empty. "
  373. "Transfer Speed is set.\n", __func__);
  374. /* validate Tx/Rx buffers and Transfer length */
  375. list_for_each_entry(transfer, &pmsg->transfers, transfer_list) {
  376. if (!transfer->tx_buf && !transfer->rx_buf) {
  377. dev_err(&pspi->dev,
  378. "%s Tx and Rx buffer NULL\n", __func__);
  379. retval = -EINVAL;
  380. goto err_out;
  381. }
  382. if (!transfer->len) {
  383. dev_err(&pspi->dev, "%s Transfer length invalid\n",
  384. __func__);
  385. retval = -EINVAL;
  386. goto err_out;
  387. }
  388. dev_dbg(&pspi->dev, "%s Tx/Rx buffer valid. Transfer length"
  389. " valid\n", __func__);
  390. /* if baud rate hs been specified validate the same */
  391. if (transfer->speed_hz > PCH_MAX_BAUDRATE)
  392. transfer->speed_hz = PCH_MAX_BAUDRATE;
  393. /* if bits per word has been specified validate the same */
  394. if (transfer->bits_per_word) {
  395. if ((transfer->bits_per_word != 8)
  396. && (transfer->bits_per_word != 16)) {
  397. retval = -EINVAL;
  398. dev_err(&pspi->dev,
  399. "%s Invalid bits per word\n", __func__);
  400. goto err_out;
  401. }
  402. }
  403. }
  404. spin_lock_irqsave(&data->lock, flags);
  405. /* We won't process any messages if we have been asked to terminate */
  406. if (data->status == STATUS_EXITING) {
  407. dev_err(&pspi->dev, "%s status = STATUS_EXITING.\n", __func__);
  408. retval = -ESHUTDOWN;
  409. goto err_return_spinlock;
  410. }
  411. /* If suspended ,return -EINVAL */
  412. if (data->board_dat->suspend_sts) {
  413. dev_err(&pspi->dev, "%s suspend; returning EINVAL\n", __func__);
  414. retval = -EINVAL;
  415. goto err_return_spinlock;
  416. }
  417. /* set status of message */
  418. pmsg->actual_length = 0;
  419. dev_dbg(&pspi->dev, "%s - pmsg->status =%d\n", __func__, pmsg->status);
  420. pmsg->status = -EINPROGRESS;
  421. /* add message to queue */
  422. list_add_tail(&pmsg->queue, &data->queue);
  423. dev_dbg(&pspi->dev, "%s - Invoked list_add_tail\n", __func__);
  424. /* schedule work queue to run */
  425. queue_work(data->wk, &data->work);
  426. dev_dbg(&pspi->dev, "%s - Invoked queue work\n", __func__);
  427. retval = 0;
  428. err_return_spinlock:
  429. spin_unlock_irqrestore(&data->lock, flags);
  430. err_out:
  431. dev_dbg(&pspi->dev, "%s RETURN=%d\n", __func__, retval);
  432. return retval;
  433. }
  434. static inline void pch_spi_select_chip(struct pch_spi_data *data,
  435. struct spi_device *pspi)
  436. {
  437. if (data->current_chip != NULL) {
  438. if (pspi->chip_select != data->n_curnt_chip) {
  439. dev_dbg(&pspi->dev, "%s : different slave\n", __func__);
  440. data->current_chip = NULL;
  441. }
  442. }
  443. data->current_chip = pspi;
  444. data->n_curnt_chip = data->current_chip->chip_select;
  445. dev_dbg(&pspi->dev, "%s :Invoking pch_spi_setup_transfer\n", __func__);
  446. pch_spi_setup_transfer(pspi);
  447. }
  448. static void pch_spi_set_tx(struct pch_spi_data *data, int *bpw,
  449. struct spi_message **ppmsg)
  450. {
  451. int size;
  452. u32 n_writes;
  453. int j;
  454. struct spi_message *pmsg;
  455. const u8 *tx_buf;
  456. const u16 *tx_sbuf;
  457. pmsg = *ppmsg;
  458. /* set baud rate if needed */
  459. if (data->cur_trans->speed_hz) {
  460. dev_dbg(&data->master->dev, "%s:setting baud rate\n", __func__);
  461. pch_spi_set_baud_rate(data->master, data->cur_trans->speed_hz);
  462. }
  463. /* set bits per word if needed */
  464. if (data->cur_trans->bits_per_word &&
  465. (data->current_msg->spi->bits_per_word != data->cur_trans->bits_per_word)) {
  466. dev_dbg(&data->master->dev, "%s:set bits per word\n", __func__);
  467. pch_spi_set_bits_per_word(data->master,
  468. data->cur_trans->bits_per_word);
  469. *bpw = data->cur_trans->bits_per_word;
  470. } else {
  471. *bpw = data->current_msg->spi->bits_per_word;
  472. }
  473. /* reset Tx/Rx index */
  474. data->tx_index = 0;
  475. data->rx_index = 0;
  476. data->bpw_len = data->cur_trans->len / (*bpw / 8);
  477. /* find alloc size */
  478. size = data->cur_trans->len * sizeof(*data->pkt_tx_buff);
  479. /* allocate memory for pkt_tx_buff & pkt_rx_buffer */
  480. data->pkt_tx_buff = kzalloc(size, GFP_KERNEL);
  481. if (data->pkt_tx_buff != NULL) {
  482. data->pkt_rx_buff = kzalloc(size, GFP_KERNEL);
  483. if (!data->pkt_rx_buff)
  484. kfree(data->pkt_tx_buff);
  485. }
  486. if (!data->pkt_rx_buff) {
  487. /* flush queue and set status of all transfers to -ENOMEM */
  488. dev_err(&data->master->dev, "%s :kzalloc failed\n", __func__);
  489. list_for_each_entry(pmsg, data->queue.next, queue) {
  490. pmsg->status = -ENOMEM;
  491. if (pmsg->complete != 0)
  492. pmsg->complete(pmsg->context);
  493. /* delete from queue */
  494. list_del_init(&pmsg->queue);
  495. }
  496. return;
  497. }
  498. /* copy Tx Data */
  499. if (data->cur_trans->tx_buf != NULL) {
  500. if (*bpw == 8) {
  501. tx_buf = data->cur_trans->tx_buf;
  502. for (j = 0; j < data->bpw_len; j++)
  503. data->pkt_tx_buff[j] = *tx_buf++;
  504. } else {
  505. tx_sbuf = data->cur_trans->tx_buf;
  506. for (j = 0; j < data->bpw_len; j++)
  507. data->pkt_tx_buff[j] = *tx_sbuf++;
  508. }
  509. }
  510. /* if len greater than PCH_MAX_FIFO_DEPTH, write 16,else len bytes */
  511. n_writes = data->bpw_len;
  512. if (n_writes > PCH_MAX_FIFO_DEPTH)
  513. n_writes = PCH_MAX_FIFO_DEPTH;
  514. dev_dbg(&data->master->dev, "\n%s:Pulling down SSN low - writing "
  515. "0x2 to SSNXCR\n", __func__);
  516. pch_spi_writereg(data->master, PCH_SSNXCR, SSN_LOW);
  517. for (j = 0; j < n_writes; j++)
  518. pch_spi_writereg(data->master, PCH_SPDWR, data->pkt_tx_buff[j]);
  519. /* update tx_index */
  520. data->tx_index = j;
  521. /* reset transfer complete flag */
  522. data->transfer_complete = false;
  523. data->transfer_active = true;
  524. }
  525. static void pch_spi_nomore_transfer(struct pch_spi_data *data,
  526. struct spi_message *pmsg)
  527. {
  528. dev_dbg(&data->master->dev, "%s called\n", __func__);
  529. /* Invoke complete callback
  530. * [To the spi core..indicating end of transfer] */
  531. data->current_msg->status = 0;
  532. if (data->current_msg->complete != 0) {
  533. dev_dbg(&data->master->dev,
  534. "%s:Invoking callback of SPI core\n", __func__);
  535. data->current_msg->complete(data->current_msg->context);
  536. }
  537. /* update status in global variable */
  538. data->bcurrent_msg_processing = false;
  539. dev_dbg(&data->master->dev,
  540. "%s:data->bcurrent_msg_processing = false\n", __func__);
  541. data->current_msg = NULL;
  542. data->cur_trans = NULL;
  543. /* check if we have items in list and not suspending
  544. * return 1 if list empty */
  545. if ((list_empty(&data->queue) == 0) &&
  546. (!data->board_dat->suspend_sts) &&
  547. (data->status != STATUS_EXITING)) {
  548. /* We have some more work to do (either there is more tranint
  549. * bpw;sfer requests in the current message or there are
  550. *more messages)
  551. */
  552. dev_dbg(&data->master->dev, "%s:Invoke queue_work\n", __func__);
  553. queue_work(data->wk, &data->work);
  554. } else if (data->board_dat->suspend_sts ||
  555. data->status == STATUS_EXITING) {
  556. dev_dbg(&data->master->dev,
  557. "%s suspend/remove initiated, flushing queue\n",
  558. __func__);
  559. list_for_each_entry(pmsg, data->queue.next, queue) {
  560. pmsg->status = -EIO;
  561. if (pmsg->complete)
  562. pmsg->complete(pmsg->context);
  563. /* delete from queue */
  564. list_del_init(&pmsg->queue);
  565. }
  566. }
  567. }
  568. static void pch_spi_set_ir(struct pch_spi_data *data)
  569. {
  570. /* enable interrupts */
  571. if ((data->bpw_len) > PCH_MAX_FIFO_DEPTH) {
  572. /* set receive threshold to PCH_RX_THOLD */
  573. pch_spi_setclr_reg(data->master, PCH_SPCR,
  574. PCH_RX_THOLD << SPCR_RFIC_FIELD,
  575. ~MASK_RFIC_SPCR_BITS);
  576. /* enable FI and RFI interrupts */
  577. pch_spi_setclr_reg(data->master, PCH_SPCR,
  578. SPCR_RFIE_BIT | SPCR_FIE_BIT, 0);
  579. } else {
  580. /* set receive threshold to maximum */
  581. pch_spi_setclr_reg(data->master, PCH_SPCR,
  582. PCH_RX_THOLD_MAX << SPCR_TFIC_FIELD,
  583. ~MASK_TFIC_SPCR_BITS);
  584. /* enable FI interrupt */
  585. pch_spi_setclr_reg(data->master, PCH_SPCR, SPCR_FIE_BIT, 0);
  586. }
  587. dev_dbg(&data->master->dev,
  588. "%s:invoking pch_spi_set_enable to enable SPI\n", __func__);
  589. /* SPI set enable */
  590. pch_spi_setclr_reg(data->current_chip->master, PCH_SPCR, SPCR_SPE_BIT, 0);
  591. /* Wait until the transfer completes; go to sleep after
  592. initiating the transfer. */
  593. dev_dbg(&data->master->dev,
  594. "%s:waiting for transfer to get over\n", __func__);
  595. wait_event_interruptible(data->wait, data->transfer_complete);
  596. pch_spi_writereg(data->master, PCH_SSNXCR, SSN_NO_CONTROL);
  597. dev_dbg(&data->master->dev,
  598. "%s:no more control over SSN-writing 0 to SSNXCR.", __func__);
  599. data->transfer_active = false;
  600. dev_dbg(&data->master->dev,
  601. "%s set data->transfer_active = false\n", __func__);
  602. /* clear all interrupts */
  603. pch_spi_writereg(data->master, PCH_SPSR,
  604. pch_spi_readreg(data->master, PCH_SPSR));
  605. /* disable interrupts */
  606. pch_spi_setclr_reg(data->master, PCH_SPCR, 0, PCH_ALL);
  607. }
  608. static void pch_spi_copy_rx_data(struct pch_spi_data *data, int bpw)
  609. {
  610. int j;
  611. u8 *rx_buf;
  612. u16 *rx_sbuf;
  613. /* copy Rx Data */
  614. if (!data->cur_trans->rx_buf)
  615. return;
  616. if (bpw == 8) {
  617. rx_buf = data->cur_trans->rx_buf;
  618. for (j = 0; j < data->bpw_len; j++)
  619. *rx_buf++ = data->pkt_rx_buff[j] & 0xFF;
  620. } else {
  621. rx_sbuf = data->cur_trans->rx_buf;
  622. for (j = 0; j < data->bpw_len; j++)
  623. *rx_sbuf++ = data->pkt_rx_buff[j];
  624. }
  625. }
  626. static void pch_spi_process_messages(struct work_struct *pwork)
  627. {
  628. struct spi_message *pmsg;
  629. struct pch_spi_data *data;
  630. int bpw;
  631. data = container_of(pwork, struct pch_spi_data, work);
  632. dev_dbg(&data->master->dev, "%s data initialized\n", __func__);
  633. spin_lock(&data->lock);
  634. /* check if suspend has been initiated;if yes flush queue */
  635. if (data->board_dat->suspend_sts || (data->status == STATUS_EXITING)) {
  636. dev_dbg(&data->master->dev,
  637. "%s suspend/remove initiated,flushing queue\n",
  638. __func__);
  639. list_for_each_entry(pmsg, data->queue.next, queue) {
  640. pmsg->status = -EIO;
  641. if (pmsg->complete != 0) {
  642. spin_unlock(&data->lock);
  643. pmsg->complete(pmsg->context);
  644. spin_lock(&data->lock);
  645. }
  646. /* delete from queue */
  647. list_del_init(&pmsg->queue);
  648. }
  649. spin_unlock(&data->lock);
  650. return;
  651. }
  652. data->bcurrent_msg_processing = true;
  653. dev_dbg(&data->master->dev,
  654. "%s Set data->bcurrent_msg_processing= true\n", __func__);
  655. /* Get the message from the queue and delete it from there. */
  656. data->current_msg = list_entry(data->queue.next, struct spi_message,
  657. queue);
  658. list_del_init(&data->current_msg->queue);
  659. data->current_msg->status = 0;
  660. pch_spi_select_chip(data, data->current_msg->spi);
  661. spin_unlock(&data->lock);
  662. do {
  663. /* If we are already processing a message get the next
  664. transfer structure from the message otherwise retrieve
  665. the 1st transfer request from the message. */
  666. spin_lock(&data->lock);
  667. if (data->cur_trans == NULL) {
  668. data->cur_trans =
  669. list_entry(data->current_msg->transfers.
  670. next, struct spi_transfer,
  671. transfer_list);
  672. dev_dbg(&data->master->dev,
  673. "%s :Getting 1st transfer message\n", __func__);
  674. } else {
  675. data->cur_trans =
  676. list_entry(data->cur_trans->transfer_list.next,
  677. struct spi_transfer,
  678. transfer_list);
  679. dev_dbg(&data->master->dev,
  680. "%s :Getting next transfer message\n",
  681. __func__);
  682. }
  683. spin_unlock(&data->lock);
  684. pch_spi_set_tx(data, &bpw, &pmsg);
  685. /* Control interrupt*/
  686. pch_spi_set_ir(data);
  687. /* Disable SPI transfer */
  688. pch_spi_setclr_reg(data->current_chip->master, PCH_SPCR, 0,
  689. SPCR_SPE_BIT);
  690. /* clear FIFO */
  691. pch_spi_clear_fifo(data->master);
  692. /* copy Rx Data */
  693. pch_spi_copy_rx_data(data, bpw);
  694. /* free memory */
  695. kfree(data->pkt_rx_buff);
  696. data->pkt_rx_buff = NULL;
  697. kfree(data->pkt_tx_buff);
  698. data->pkt_tx_buff = NULL;
  699. /* increment message count */
  700. data->current_msg->actual_length += data->cur_trans->len;
  701. dev_dbg(&data->master->dev,
  702. "%s:data->current_msg->actual_length=%d\n",
  703. __func__, data->current_msg->actual_length);
  704. /* check for delay */
  705. if (data->cur_trans->delay_usecs) {
  706. dev_dbg(&data->master->dev, "%s:"
  707. "delay in usec=%d\n", __func__,
  708. data->cur_trans->delay_usecs);
  709. udelay(data->cur_trans->delay_usecs);
  710. }
  711. spin_lock(&data->lock);
  712. /* No more transfer in this message. */
  713. if ((data->cur_trans->transfer_list.next) ==
  714. &(data->current_msg->transfers)) {
  715. pch_spi_nomore_transfer(data, pmsg);
  716. }
  717. spin_unlock(&data->lock);
  718. } while (data->cur_trans != NULL);
  719. }
  720. static void pch_spi_free_resources(struct pch_spi_board_data *board_dat)
  721. {
  722. dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);
  723. /* free workqueue */
  724. if (board_dat->data->wk != NULL) {
  725. destroy_workqueue(board_dat->data->wk);
  726. board_dat->data->wk = NULL;
  727. dev_dbg(&board_dat->pdev->dev,
  728. "%s destroy_workqueue invoked successfully\n",
  729. __func__);
  730. }
  731. /* disable interrupts & free IRQ */
  732. if (board_dat->irq_reg_sts) {
  733. /* disable interrupts */
  734. pch_spi_setclr_reg(board_dat->data->master, PCH_SPCR, 0,
  735. PCH_ALL);
  736. /* free IRQ */
  737. free_irq(board_dat->pdev->irq, board_dat);
  738. dev_dbg(&board_dat->pdev->dev,
  739. "%s free_irq invoked successfully\n", __func__);
  740. board_dat->irq_reg_sts = false;
  741. }
  742. /* unmap PCI base address */
  743. if (board_dat->data->io_remap_addr != 0) {
  744. pci_iounmap(board_dat->pdev, board_dat->data->io_remap_addr);
  745. board_dat->data->io_remap_addr = 0;
  746. dev_dbg(&board_dat->pdev->dev,
  747. "%s pci_iounmap invoked successfully\n", __func__);
  748. }
  749. /* release PCI region */
  750. if (board_dat->pci_req_sts) {
  751. pci_release_regions(board_dat->pdev);
  752. dev_dbg(&board_dat->pdev->dev,
  753. "%s pci_release_regions invoked successfully\n",
  754. __func__);
  755. board_dat->pci_req_sts = false;
  756. }
  757. }
  758. static int pch_spi_get_resources(struct pch_spi_board_data *board_dat)
  759. {
  760. void __iomem *io_remap_addr;
  761. int retval;
  762. dev_dbg(&board_dat->pdev->dev, "%s ENTRY\n", __func__);
  763. /* create workqueue */
  764. board_dat->data->wk = create_singlethread_workqueue(KBUILD_MODNAME);
  765. if (!board_dat->data->wk) {
  766. dev_err(&board_dat->pdev->dev,
  767. "%s create_singlet hread_workqueue failed\n", __func__);
  768. retval = -EBUSY;
  769. goto err_return;
  770. }
  771. dev_dbg(&board_dat->pdev->dev,
  772. "%s create_singlethread_workqueue success\n", __func__);
  773. retval = pci_request_regions(board_dat->pdev, KBUILD_MODNAME);
  774. if (retval != 0) {
  775. dev_err(&board_dat->pdev->dev,
  776. "%s request_region failed\n", __func__);
  777. goto err_return;
  778. }
  779. board_dat->pci_req_sts = true;
  780. io_remap_addr = pci_iomap(board_dat->pdev, 1, 0);
  781. if (io_remap_addr == 0) {
  782. dev_err(&board_dat->pdev->dev,
  783. "%s pci_iomap failed\n", __func__);
  784. retval = -ENOMEM;
  785. goto err_return;
  786. }
  787. /* calculate base address for all channels */
  788. board_dat->data->io_remap_addr = io_remap_addr;
  789. /* reset PCH SPI h/w */
  790. pch_spi_reset(board_dat->data->master);
  791. dev_dbg(&board_dat->pdev->dev,
  792. "%s pch_spi_reset invoked successfully\n", __func__);
  793. /* register IRQ */
  794. retval = request_irq(board_dat->pdev->irq, pch_spi_handler,
  795. IRQF_SHARED, KBUILD_MODNAME, board_dat);
  796. if (retval != 0) {
  797. dev_err(&board_dat->pdev->dev,
  798. "%s request_irq failed\n", __func__);
  799. goto err_return;
  800. }
  801. dev_dbg(&board_dat->pdev->dev, "%s request_irq returned=%d\n",
  802. __func__, retval);
  803. board_dat->irq_reg_sts = true;
  804. dev_dbg(&board_dat->pdev->dev, "%s data->irq_reg_sts=true\n", __func__);
  805. err_return:
  806. if (retval != 0) {
  807. dev_err(&board_dat->pdev->dev,
  808. "%s FAIL:invoking pch_spi_free_resources\n", __func__);
  809. pch_spi_free_resources(board_dat);
  810. }
  811. dev_dbg(&board_dat->pdev->dev, "%s Return=%d\n", __func__, retval);
  812. return retval;
  813. }
  814. static int pch_spi_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  815. {
  816. struct spi_master *master;
  817. struct pch_spi_board_data *board_dat;
  818. int retval;
  819. dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
  820. /* allocate memory for private data */
  821. board_dat = kzalloc(sizeof(struct pch_spi_board_data), GFP_KERNEL);
  822. if (board_dat == NULL) {
  823. dev_err(&pdev->dev,
  824. " %s memory allocation for private data failed\n",
  825. __func__);
  826. retval = -ENOMEM;
  827. goto err_kmalloc;
  828. }
  829. dev_dbg(&pdev->dev,
  830. "%s memory allocation for private data success\n", __func__);
  831. /* enable PCI device */
  832. retval = pci_enable_device(pdev);
  833. if (retval != 0) {
  834. dev_err(&pdev->dev, "%s pci_enable_device FAILED\n", __func__);
  835. goto err_pci_en_device;
  836. }
  837. dev_dbg(&pdev->dev, "%s pci_enable_device returned=%d\n",
  838. __func__, retval);
  839. board_dat->pdev = pdev;
  840. /* alllocate memory for SPI master */
  841. master = spi_alloc_master(&pdev->dev, sizeof(struct pch_spi_data));
  842. if (master == NULL) {
  843. retval = -ENOMEM;
  844. dev_err(&pdev->dev, "%s Fail.\n", __func__);
  845. goto err_spi_alloc_master;
  846. }
  847. dev_dbg(&pdev->dev,
  848. "%s spi_alloc_master returned non NULL\n", __func__);
  849. /* initialize members of SPI master */
  850. master->bus_num = -1;
  851. master->num_chipselect = PCH_MAX_CS;
  852. master->setup = pch_spi_setup;
  853. master->transfer = pch_spi_transfer;
  854. dev_dbg(&pdev->dev,
  855. "%s transfer member of SPI master initialized\n", __func__);
  856. board_dat->data = spi_master_get_devdata(master);
  857. board_dat->data->master = master;
  858. board_dat->data->n_curnt_chip = 255;
  859. board_dat->data->board_dat = board_dat;
  860. board_dat->data->status = STATUS_RUNNING;
  861. INIT_LIST_HEAD(&board_dat->data->queue);
  862. spin_lock_init(&board_dat->data->lock);
  863. INIT_WORK(&board_dat->data->work, pch_spi_process_messages);
  864. init_waitqueue_head(&board_dat->data->wait);
  865. /* allocate resources for PCH SPI */
  866. retval = pch_spi_get_resources(board_dat);
  867. if (retval) {
  868. dev_err(&pdev->dev, "%s fail(retval=%d)\n", __func__, retval);
  869. goto err_spi_get_resources;
  870. }
  871. dev_dbg(&pdev->dev, "%s pch_spi_get_resources returned=%d\n",
  872. __func__, retval);
  873. /* save private data in dev */
  874. pci_set_drvdata(pdev, board_dat);
  875. dev_dbg(&pdev->dev, "%s invoked pci_set_drvdata\n", __func__);
  876. /* set master mode */
  877. pch_spi_set_master_mode(master);
  878. dev_dbg(&pdev->dev,
  879. "%s invoked pch_spi_set_master_mode\n", __func__);
  880. /* Register the controller with the SPI core. */
  881. retval = spi_register_master(master);
  882. if (retval != 0) {
  883. dev_err(&pdev->dev,
  884. "%s spi_register_master FAILED\n", __func__);
  885. goto err_spi_reg_master;
  886. }
  887. dev_dbg(&pdev->dev, "%s spi_register_master returned=%d\n",
  888. __func__, retval);
  889. return 0;
  890. err_spi_reg_master:
  891. spi_unregister_master(master);
  892. err_spi_get_resources:
  893. err_spi_alloc_master:
  894. spi_master_put(master);
  895. pci_disable_device(pdev);
  896. err_pci_en_device:
  897. kfree(board_dat);
  898. err_kmalloc:
  899. return retval;
  900. }
  901. static void pch_spi_remove(struct pci_dev *pdev)
  902. {
  903. struct pch_spi_board_data *board_dat = pci_get_drvdata(pdev);
  904. int count;
  905. dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
  906. if (!board_dat) {
  907. dev_err(&pdev->dev,
  908. "%s pci_get_drvdata returned NULL\n", __func__);
  909. return;
  910. }
  911. /* check for any pending messages; no action is taken if the queue
  912. * is still full; but at least we tried. Unload anyway */
  913. count = 500;
  914. spin_lock(&board_dat->data->lock);
  915. board_dat->data->status = STATUS_EXITING;
  916. while ((list_empty(&board_dat->data->queue) == 0) && --count) {
  917. dev_dbg(&board_dat->pdev->dev, "%s :queue not empty\n",
  918. __func__);
  919. spin_unlock(&board_dat->data->lock);
  920. msleep(PCH_SLEEP_TIME);
  921. spin_lock(&board_dat->data->lock);
  922. }
  923. spin_unlock(&board_dat->data->lock);
  924. /* Free resources allocated for PCH SPI */
  925. pch_spi_free_resources(board_dat);
  926. spi_unregister_master(board_dat->data->master);
  927. /* free memory for private data */
  928. kfree(board_dat);
  929. pci_set_drvdata(pdev, NULL);
  930. /* disable PCI device */
  931. pci_disable_device(pdev);
  932. dev_dbg(&pdev->dev, "%s invoked pci_disable_device\n", __func__);
  933. }
  934. #ifdef CONFIG_PM
  935. static int pch_spi_suspend(struct pci_dev *pdev, pm_message_t state)
  936. {
  937. u8 count;
  938. int retval;
  939. struct pch_spi_board_data *board_dat = pci_get_drvdata(pdev);
  940. dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
  941. if (!board_dat) {
  942. dev_err(&pdev->dev,
  943. "%s pci_get_drvdata returned NULL\n", __func__);
  944. return -EFAULT;
  945. }
  946. retval = 0;
  947. board_dat->suspend_sts = true;
  948. /* check if the current message is processed:
  949. Only after thats done the transfer will be suspended */
  950. count = 255;
  951. while ((--count) > 0) {
  952. if (!(board_dat->data->bcurrent_msg_processing)) {
  953. dev_dbg(&pdev->dev, "%s board_dat->data->bCurrent_"
  954. "msg_processing = false\n", __func__);
  955. break;
  956. } else {
  957. dev_dbg(&pdev->dev, "%s board_dat->data->bCurrent_msg_"
  958. "processing = true\n", __func__);
  959. }
  960. msleep(PCH_SLEEP_TIME);
  961. }
  962. /* Free IRQ */
  963. if (board_dat->irq_reg_sts) {
  964. /* disable all interrupts */
  965. pch_spi_setclr_reg(board_dat->data->master, PCH_SPCR, 0,
  966. PCH_ALL);
  967. pch_spi_reset(board_dat->data->master);
  968. free_irq(board_dat->pdev->irq, board_dat);
  969. board_dat->irq_reg_sts = false;
  970. dev_dbg(&pdev->dev,
  971. "%s free_irq invoked successfully.\n", __func__);
  972. }
  973. /* save config space */
  974. retval = pci_save_state(pdev);
  975. if (retval == 0) {
  976. dev_dbg(&pdev->dev, "%s pci_save_state returned=%d\n",
  977. __func__, retval);
  978. /* disable PM notifications */
  979. pci_enable_wake(pdev, PCI_D3hot, 0);
  980. dev_dbg(&pdev->dev,
  981. "%s pci_enable_wake invoked successfully\n", __func__);
  982. /* disable PCI device */
  983. pci_disable_device(pdev);
  984. dev_dbg(&pdev->dev,
  985. "%s pci_disable_device invoked successfully\n",
  986. __func__);
  987. /* move device to D3hot state */
  988. pci_set_power_state(pdev, PCI_D3hot);
  989. dev_dbg(&pdev->dev,
  990. "%s pci_set_power_state invoked successfully\n",
  991. __func__);
  992. } else {
  993. dev_err(&pdev->dev, "%s pci_save_state failed\n", __func__);
  994. }
  995. dev_dbg(&pdev->dev, "%s return=%d\n", __func__, retval);
  996. return retval;
  997. }
  998. static int pch_spi_resume(struct pci_dev *pdev)
  999. {
  1000. int retval;
  1001. struct pch_spi_board_data *board = pci_get_drvdata(pdev);
  1002. dev_dbg(&pdev->dev, "%s ENTRY\n", __func__);
  1003. if (!board) {
  1004. dev_err(&pdev->dev,
  1005. "%s pci_get_drvdata returned NULL\n", __func__);
  1006. return -EFAULT;
  1007. }
  1008. /* move device to DO power state */
  1009. pci_set_power_state(pdev, PCI_D0);
  1010. /* restore state */
  1011. pci_restore_state(pdev);
  1012. retval = pci_enable_device(pdev);
  1013. if (retval < 0) {
  1014. dev_err(&pdev->dev,
  1015. "%s pci_enable_device failed\n", __func__);
  1016. } else {
  1017. /* disable PM notifications */
  1018. pci_enable_wake(pdev, PCI_D3hot, 0);
  1019. /* register IRQ handler */
  1020. if (!board->irq_reg_sts) {
  1021. /* register IRQ */
  1022. retval = request_irq(board->pdev->irq, pch_spi_handler,
  1023. IRQF_SHARED, KBUILD_MODNAME,
  1024. board);
  1025. if (retval < 0) {
  1026. dev_err(&pdev->dev,
  1027. "%s request_irq failed\n", __func__);
  1028. return retval;
  1029. }
  1030. board->irq_reg_sts = true;
  1031. /* reset PCH SPI h/w */
  1032. pch_spi_reset(board->data->master);
  1033. pch_spi_set_master_mode(board->data->master);
  1034. /* set suspend status to false */
  1035. board->suspend_sts = false;
  1036. }
  1037. }
  1038. dev_dbg(&pdev->dev, "%s returning=%d\n", __func__, retval);
  1039. return retval;
  1040. }
  1041. #else
  1042. #define pch_spi_suspend NULL
  1043. #define pch_spi_resume NULL
  1044. #endif
  1045. static struct pci_driver pch_spi_pcidev = {
  1046. .name = "pch_spi",
  1047. .id_table = pch_spi_pcidev_id,
  1048. .probe = pch_spi_probe,
  1049. .remove = pch_spi_remove,
  1050. .suspend = pch_spi_suspend,
  1051. .resume = pch_spi_resume,
  1052. };
  1053. static int __init pch_spi_init(void)
  1054. {
  1055. return pci_register_driver(&pch_spi_pcidev);
  1056. }
  1057. module_init(pch_spi_init);
  1058. static void __exit pch_spi_exit(void)
  1059. {
  1060. pci_unregister_driver(&pch_spi_pcidev);
  1061. }
  1062. module_exit(pch_spi_exit);
  1063. MODULE_LICENSE("GPL");
  1064. MODULE_DESCRIPTION("Topcliff PCH SPI PCI Driver");