2 * SuperH HSPI bus driver
4 * Copyright (C) 2011 Kuninori Morimoto
7 * Based on pxa2xx_spi.c:
8 * Copyright (C) 2011 Renesas Solutions Corp.
9 * Copyright (C) 2005 Stephen Street / StreetFire Sound Labs
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <linux/clk.h>
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/timer.h>
30 #include <linux/delay.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
33 #include <linux/platform_device.h>
34 #include <linux/pm_runtime.h>
36 #include <linux/spi/spi.h>
37 #include <linux/spi/sh_hspi.h>
49 #define hspi2info(h) (h->dev->platform_data)
53 struct spi_master
*master
;
61 static void hspi_write(struct hspi_priv
*hspi
, int reg
, u32 val
)
63 iowrite32(val
, hspi
->addr
+ reg
);
66 static u32
hspi_read(struct hspi_priv
*hspi
, int reg
)
68 return ioread32(hspi
->addr
+ reg
);
74 static int hspi_status_check_timeout(struct hspi_priv
*hspi
, u32 mask
, u32 val
)
79 if ((mask
& hspi_read(hspi
, SPSR
)) == val
)
85 dev_err(hspi
->dev
, "timeout\n");
92 static int hspi_prepare_transfer(struct spi_master
*master
)
94 struct hspi_priv
*hspi
= spi_master_get_devdata(master
);
96 pm_runtime_get_sync(hspi
->dev
);
100 static int hspi_unprepare_transfer(struct spi_master
*master
)
102 struct hspi_priv
*hspi
= spi_master_get_devdata(master
);
104 pm_runtime_put_sync(hspi
->dev
);
108 static void hspi_hw_setup(struct hspi_priv
*hspi
,
109 struct spi_message
*msg
,
110 struct spi_transfer
*t
)
112 struct spi_device
*spi
= msg
->spi
;
113 struct device
*dev
= hspi
->dev
;
116 u32 rate
, best_rate
, min
, tmp
;
118 target_rate
= t
? t
->speed_hz
: 0;
120 target_rate
= spi
->max_speed_hz
;
123 * find best IDIV/CLKCx settings
128 for (idiv_clk
= 0x00; idiv_clk
<= 0x3F; idiv_clk
++) {
129 rate
= clk_get_rate(hspi
->clk
);
131 /* IDIV calculation */
132 if (idiv_clk
& (1 << 5))
137 /* CLKCx calculation */
138 rate
/= (((idiv_clk
& 0x1F) + 1) * 2) ;
140 /* save best settings */
141 tmp
= abs(target_rate
- rate
);
149 if (spi
->mode
& SPI_CPHA
)
151 if (spi
->mode
& SPI_CPOL
)
154 dev_dbg(dev
, "speed %d/%d\n", target_rate
, best_rate
);
156 hspi_write(hspi
, SPCR
, spcr
);
157 hspi_write(hspi
, SPSR
, 0x0);
158 hspi_write(hspi
, SPSCR
, 0x1); /* master mode */
161 static int hspi_transfer_one_message(struct spi_master
*master
,
162 struct spi_message
*msg
)
164 struct hspi_priv
*hspi
= spi_master_get_devdata(master
);
165 struct spi_transfer
*t
;
170 dev_dbg(hspi
->dev
, "%s\n", __func__
);
173 list_for_each_entry(t
, &msg
->transfers
, transfer_list
) {
174 hspi_hw_setup(hspi
, msg
, t
);
176 for (i
= 0; i
< t
->len
; i
++) {
179 ret
= hspi_status_check_timeout(hspi
, 0x1, 0);
185 tx
= (u32
)((u8
*)t
->tx_buf
)[i
];
187 hspi_write(hspi
, SPTBR
, tx
);
190 ret
= hspi_status_check_timeout(hspi
, 0x4, 0x4);
194 rx
= hspi_read(hspi
, SPRBR
);
196 ((u8
*)t
->rx_buf
)[i
] = (u8
)rx
;
200 msg
->actual_length
+= t
->len
;
204 spi_finalize_current_message(master
);
209 static int hspi_setup(struct spi_device
*spi
)
211 struct hspi_priv
*hspi
= spi_master_get_devdata(spi
->master
);
212 struct device
*dev
= hspi
->dev
;
214 if (8 != spi
->bits_per_word
) {
215 dev_err(dev
, "bits_per_word should be 8\n");
219 dev_dbg(dev
, "%s setup\n", spi
->modalias
);
224 static void hspi_cleanup(struct spi_device
*spi
)
226 struct hspi_priv
*hspi
= spi_master_get_devdata(spi
->master
);
227 struct device
*dev
= hspi
->dev
;
229 dev_dbg(dev
, "%s cleanup\n", spi
->modalias
);
232 static int __devinit
hspi_probe(struct platform_device
*pdev
)
234 struct resource
*res
;
235 struct spi_master
*master
;
236 struct hspi_priv
*hspi
;
241 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
243 dev_err(&pdev
->dev
, "invalid resource\n");
247 master
= spi_alloc_master(&pdev
->dev
, sizeof(*hspi
));
249 dev_err(&pdev
->dev
, "spi_alloc_master error.\n");
253 clk
= clk_get(NULL
, "shyway_clk");
255 dev_err(&pdev
->dev
, "shyway_clk is required\n");
260 hspi
= spi_master_get_devdata(master
);
261 dev_set_drvdata(&pdev
->dev
, hspi
);
264 hspi
->master
= master
;
265 hspi
->dev
= &pdev
->dev
;
267 hspi
->addr
= devm_ioremap(hspi
->dev
,
268 res
->start
, resource_size(res
));
270 dev_err(&pdev
->dev
, "ioremap error.\n");
275 master
->num_chipselect
= 1;
276 master
->bus_num
= pdev
->id
;
277 master
->setup
= hspi_setup
;
278 master
->cleanup
= hspi_cleanup
;
279 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
;
280 master
->prepare_transfer_hardware
= hspi_prepare_transfer
;
281 master
->transfer_one_message
= hspi_transfer_one_message
;
282 master
->unprepare_transfer_hardware
= hspi_unprepare_transfer
;
283 ret
= spi_register_master(master
);
285 dev_err(&pdev
->dev
, "spi_register_master error.\n");
289 pm_runtime_enable(&pdev
->dev
);
291 dev_info(&pdev
->dev
, "probed\n");
296 devm_iounmap(hspi
->dev
, hspi
->addr
);
300 spi_master_put(master
);
305 static int __devexit
hspi_remove(struct platform_device
*pdev
)
307 struct hspi_priv
*hspi
= dev_get_drvdata(&pdev
->dev
);
309 pm_runtime_disable(&pdev
->dev
);
312 spi_unregister_master(hspi
->master
);
313 devm_iounmap(hspi
->dev
, hspi
->addr
);
318 static struct platform_driver hspi_driver
= {
320 .remove
= __devexit_p(hspi_remove
),
323 .owner
= THIS_MODULE
,
326 module_platform_driver(hspi_driver
);
328 MODULE_DESCRIPTION("SuperH HSPI bus driver");
329 MODULE_LICENSE("GPL");
330 MODULE_AUTHOR("Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>");
331 MODULE_ALIAS("platform:sh_spi");