2 * Copyright (c) 2017 Rogiel Sulzbach <rogiel@allogica.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/param.h>
29 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
35 #include <machine/bus.h>
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
39 #include <dev/ahci/ahci.h>
40 #include <arm/freescale/imx/imx_iomuxreg.h>
41 #include <arm/freescale/imx/imx_iomuxvar.h>
42 #include <arm/freescale/imx/imx_ccmvar.h>
44 #define SATA_TIMER1MS 0x000000e0
46 #define SATA_P0PHYCR 0x00000178
47 #define SATA_P0PHYCR_CR_READ (1 << 19)
48 #define SATA_P0PHYCR_CR_WRITE (1 << 18)
49 #define SATA_P0PHYCR_CR_CAP_DATA (1 << 17)
50 #define SATA_P0PHYCR_CR_CAP_ADDR (1 << 16)
51 #define SATA_P0PHYCR_CR_DATA_IN(v) ((v) & 0xffff)
53 #define SATA_P0PHYSR 0x0000017c
54 #define SATA_P0PHYSR_CR_ACK (1 << 18)
55 #define SATA_P0PHYSR_CR_DATA_OUT(v) ((v) & 0xffff)
58 #define SATA_PHY_CLOCK_RESET 0x7f3f
59 #define SATA_PHY_CLOCK_RESET_RST (1 << 0)
61 #define SATA_PHY_LANE0_OUT_STAT 0x2003
62 #define SATA_PHY_LANE0_OUT_STAT_RX_PLL_STATE (1 << 1)
64 static struct ofw_compat_data compat_data[] = {
65 {"fsl,imx6q-ahci", true},
70 imx6_ahci_phy_ctrl(struct ahci_controller* sc, uint32_t bitmask, bool on)
76 v = ATA_INL(sc->r_mem, SATA_P0PHYCR);
82 ATA_OUTL(sc->r_mem, SATA_P0PHYCR, v);
84 for (timeout = 5000; timeout > 0; --timeout) {
85 v = ATA_INL(sc->r_mem, SATA_P0PHYSR);
86 state = (v & SATA_P0PHYSR_CR_ACK) == SATA_P0PHYSR_CR_ACK;
101 imx6_ahci_phy_addr(struct ahci_controller* sc, uint32_t addr)
107 ATA_OUTL(sc->r_mem, SATA_P0PHYCR, addr);
109 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_CAP_ADDR, true);
111 device_printf(sc->dev,
112 "%s: timeout on SATA_P0PHYCR_CR_CAP_ADDR=1\n",
117 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_CAP_ADDR, false);
119 device_printf(sc->dev,
120 "%s: timeout on SATA_P0PHYCR_CR_CAP_ADDR=0\n",
129 imx6_ahci_phy_write(struct ahci_controller* sc, uint32_t addr,
134 error = imx6_ahci_phy_addr(sc, addr);
136 device_printf(sc->dev, "%s: error on imx6_ahci_phy_addr\n",
141 ATA_OUTL(sc->r_mem, SATA_P0PHYCR, data);
143 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_CAP_DATA, true);
145 device_printf(sc->dev,
146 "%s: error on SATA_P0PHYCR_CR_CAP_DATA=1\n", __FUNCTION__);
149 if (imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_CAP_DATA, false) != 0) {
150 device_printf(sc->dev,
151 "%s: error on SATA_P0PHYCR_CR_CAP_DATA=0\n", __FUNCTION__);
155 if ((addr == SATA_PHY_CLOCK_RESET) && data) {
156 /* we can't check ACK after RESET */
157 ATA_OUTL(sc->r_mem, SATA_P0PHYCR,
158 SATA_P0PHYCR_CR_DATA_IN(data) | SATA_P0PHYCR_CR_WRITE);
162 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_WRITE, true);
164 device_printf(sc->dev, "%s: error on SATA_P0PHYCR_CR_WRITE=1\n",
169 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_WRITE, false);
171 device_printf(sc->dev, "%s: error on SATA_P0PHYCR_CR_WRITE=0\n",
180 imx6_ahci_phy_read(struct ahci_controller* sc, uint32_t addr, uint16_t* val)
185 error = imx6_ahci_phy_addr(sc, addr);
187 device_printf(sc->dev, "%s: error on imx6_ahci_phy_addr\n",
192 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_READ, true);
194 device_printf(sc->dev, "%s: error on SATA_P0PHYCR_CR_READ=1\n",
199 v = ATA_INL(sc->r_mem, SATA_P0PHYSR);
201 error = imx6_ahci_phy_ctrl(sc, SATA_P0PHYCR_CR_READ, false);
203 device_printf(sc->dev, "%s: error on SATA_P0PHYCR_CR_READ=0\n",
208 *val = SATA_P0PHYSR_CR_DATA_OUT(v);
213 imx6_ahci_probe(device_t dev)
216 if (!ofw_bus_status_okay(dev)) {
220 if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) {
223 device_set_desc(dev, "i.MX6 Integrated AHCI controller");
225 return (BUS_PROBE_DEFAULT);
229 imx6_ahci_attach(device_t dev)
231 struct ahci_controller* ctlr;
236 ctlr = device_get_softc(dev);
238 /* Power up the controller and phy. */
239 error = imx6_ccm_sata_enable();
241 device_printf(dev, "error enabling controller and phy\n");
247 ctlr->subvendorid = 0;
248 ctlr->subdeviceid = 0;
251 if ((ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
252 &ctlr->r_rid, RF_ACTIVE)) == NULL) {
256 v = imx_iomux_gpr_get(IOMUX_GPR13);
257 /* Clear out existing values; these numbers are bitmasks. */
258 v &= ~(IOMUX_GPR13_SATA_PHY_8(7) |
259 IOMUX_GPR13_SATA_PHY_7(0x1f) |
260 IOMUX_GPR13_SATA_PHY_6(7) |
261 IOMUX_GPR13_SATA_SPEED(1) |
262 IOMUX_GPR13_SATA_PHY_5(1) |
263 IOMUX_GPR13_SATA_PHY_4(7) |
264 IOMUX_GPR13_SATA_PHY_3(0xf) |
265 IOMUX_GPR13_SATA_PHY_2(0x1f) |
266 IOMUX_GPR13_SATA_PHY_1(1) |
267 IOMUX_GPR13_SATA_PHY_0(1));
269 v |= IOMUX_GPR13_SATA_PHY_8(5) | /* Rx 3.0db */
270 IOMUX_GPR13_SATA_PHY_7(0x12) | /* Rx SATA2m */
271 IOMUX_GPR13_SATA_PHY_6(3) | /* Rx DPLL mode */
272 IOMUX_GPR13_SATA_SPEED(1) | /* 3.0GHz */
273 IOMUX_GPR13_SATA_PHY_5(0) | /* SpreadSpectram */
274 IOMUX_GPR13_SATA_PHY_4(4) | /* Tx Attenuation 9/16 */
275 IOMUX_GPR13_SATA_PHY_3(0) | /* Tx Boost 0db */
276 IOMUX_GPR13_SATA_PHY_2(0x11) | /* Tx Level 1.104V */
277 IOMUX_GPR13_SATA_PHY_1(1); /* PLL clock enable */
278 imx_iomux_gpr_set(IOMUX_GPR13, v);
281 error = imx6_ahci_phy_write(ctlr, SATA_PHY_CLOCK_RESET,
282 SATA_PHY_CLOCK_RESET_RST);
284 device_printf(dev, "cannot reset PHY\n");
288 for (timeout = 50; timeout > 0; --timeout) {
290 error = imx6_ahci_phy_read(ctlr, SATA_PHY_LANE0_OUT_STAT,
293 device_printf(dev, "cannot read LANE0 status\n");
296 if (pllstat & SATA_PHY_LANE0_OUT_STAT_RX_PLL_STATE) {
301 device_printf(dev, "time out reading LANE0 status\n");
306 /* Support Staggered Spin-up */
307 v = ATA_INL(ctlr->r_mem, AHCI_CAP);
308 ATA_OUTL(ctlr->r_mem, AHCI_CAP, v | AHCI_CAP_SSS);
310 /* Ports Implemented. must set 1 */
311 v = ATA_INL(ctlr->r_mem, AHCI_PI);
312 ATA_OUTL(ctlr->r_mem, AHCI_PI, v | (1 << 0));
314 /* set 1ms-timer = AHB clock / 1000 */
315 ATA_OUTL(ctlr->r_mem, SATA_TIMER1MS,
316 imx_ccm_ahb_hz() / 1000);
319 * Note: ahci_attach will release ctlr->r_mem on errors automatically
321 return (ahci_attach(dev));
324 bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
329 imx6_ahci_detach(device_t dev)
332 return (ahci_detach(dev));
335 static device_method_t imx6_ahci_ata_methods[] = {
336 /* device probe, attach and detach methods */
337 DEVMETHOD(device_probe, imx6_ahci_probe),
338 DEVMETHOD(device_attach, imx6_ahci_attach),
339 DEVMETHOD(device_detach, imx6_ahci_detach),
341 /* ahci bus methods */
342 DEVMETHOD(bus_print_child, ahci_print_child),
343 DEVMETHOD(bus_alloc_resource, ahci_alloc_resource),
344 DEVMETHOD(bus_release_resource, ahci_release_resource),
345 DEVMETHOD(bus_setup_intr, ahci_setup_intr),
346 DEVMETHOD(bus_teardown_intr, ahci_teardown_intr),
347 DEVMETHOD(bus_child_location, ahci_child_location),
352 static driver_t ahci_ata_driver = {
354 imx6_ahci_ata_methods,
355 sizeof(struct ahci_controller)
358 DRIVER_MODULE(imx6_ahci, simplebus, ahci_ata_driver, 0, 0);
359 MODULE_DEPEND(imx6_ahci, ahci, 1, 1, 1);
360 SIMPLEBUS_PNP_INFO(compat_data)