#include "opt_bus.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <machine/bus.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ahci/ahci.h>
#include <dev/clk/clk.h>
#include <dev/regulator/regulator.h>
#define AHCI_BISTAFR 0x00A0
#define AHCI_BISTCR 0x00A4
#define AHCI_BISTFCTR 0x00A8
#define AHCI_BISTSR 0x00AC
#define AHCI_BISTDECR 0x00B0
#define AHCI_DIAGNR 0x00B4
#define AHCI_DIAGNR1 0x00B8
#define AHCI_OOBR 0x00BC
#define AHCI_PHYCS0R 0x00C0
#define PHYCS0R_BIT18 (1 << 18)
#define PHYCS0R_POWER_ENABLE (1 << 19)
#define PHYCS0R_UF1_MASK (7 << 20)
#define PHYCS0R_UF1_INIT (3 << 20)
#define PHYCS0R_BIT23 (1 << 23)
#define PHYCS0R_UF2_MASK (7 << 24)
#define PHYCS0R_UF2_INIT (5 << 24)
#define PHYCS0R_POWER_STATUS_MASK (7 << 28)
#define PHYCS0R_PS_GOOD (2 << 28)
#define AHCI_PHYCS1R 0x00C4
#define PHYCS1R_UF1_MASK (3 << 6)
#define PHYCS1R_UF1_INIT (2 << 6)
#define PHYCS1R_UF2_MASK (0x1f << 8)
#define PHYCS1R_UF2_INIT (6 << 8)
#define PHYCS1R_BIT15 (1 << 15)
#define PHYCS1R_UF3_MASK (3 << 16)
#define PHYCS1R_UF3_INIT (2 << 16)
#define PHYCS1R_HIGHZ (1 << 19)
#define PHYCS1R_BIT28 (1 << 28)
#define AHCI_PHYCS2R 0x00C8
#define PHYCS2R_UF1_MASK (0x1f << 5)
#define PHYCS2R_UF1_INIT (0x19 << 5)
#define PHYCS2R_CALIBRATE (1 << 24)
#define AHCI_TIMER1MS 0x00E0
#define AHCI_GPARAM1R 0x00E8
#define AHCI_GPARAM2R 0x00EC
#define AHCI_PPARAMR 0x00F0
#define AHCI_TESTR 0x00F4
#define AHCI_VERSIONR 0x00F8
#define AHCI_IDR 0x00FC
#define AHCI_RWCR 0x00FC
#define AHCI_P0DMACR 0x0070
#define AHCI_P0PHYCR 0x0078
#define AHCI_P0PHYSR 0x007C
#define PLL_FREQ 100000000
struct ahci_a10_softc {
struct ahci_controller ahci_ctlr;
regulator_t ahci_reg;
clk_t clk_pll;
clk_t clk_gate;
};
static void inline
ahci_set(struct resource *m, bus_size_t off, uint32_t set)
{
uint32_t val = ATA_INL(m, off);
val |= set;
ATA_OUTL(m, off, val);
}
static void inline
ahci_clr(struct resource *m, bus_size_t off, uint32_t clr)
{
uint32_t val = ATA_INL(m, off);
val &= ~clr;
ATA_OUTL(m, off, val);
}
static void inline
ahci_mask_set(struct resource *m, bus_size_t off, uint32_t mask, uint32_t set)
{
uint32_t val = ATA_INL(m, off);
val &= mask;
val |= set;
ATA_OUTL(m, off, val);
}
#define PHY_RESET_TIMEOUT 1000
static void
ahci_a10_phy_reset(device_t dev)
{
uint32_t to, val;
struct ahci_controller *ctlr = device_get_softc(dev);
ATA_OUTL(ctlr->r_mem, AHCI_RWCR, 0);
DELAY(100);
ahci_set(ctlr->r_mem, AHCI_PHYCS1R, PHYCS1R_HIGHZ);
ahci_mask_set(ctlr->r_mem, AHCI_PHYCS0R,
~PHYCS0R_UF2_MASK,
PHYCS0R_UF2_INIT | PHYCS0R_BIT23 | PHYCS0R_BIT18);
ahci_mask_set(ctlr->r_mem, AHCI_PHYCS1R,
~(PHYCS1R_UF1_MASK | PHYCS1R_UF2_MASK | PHYCS1R_UF3_MASK),
PHYCS1R_UF1_INIT | PHYCS1R_UF2_INIT | PHYCS1R_UF3_INIT);
ahci_set(ctlr->r_mem, AHCI_PHYCS1R, PHYCS1R_BIT15 | PHYCS1R_BIT28);
ahci_clr(ctlr->r_mem, AHCI_PHYCS1R, PHYCS1R_HIGHZ);
ahci_mask_set(ctlr->r_mem, AHCI_PHYCS0R,
~PHYCS0R_UF1_MASK, PHYCS0R_UF1_INIT);
ahci_mask_set(ctlr->r_mem, AHCI_PHYCS2R, ~PHYCS2R_UF1_MASK,
PHYCS2R_UF1_INIT);
DELAY(100);
ahci_set(ctlr->r_mem, AHCI_PHYCS0R, PHYCS0R_POWER_ENABLE);
for (to = PHY_RESET_TIMEOUT; to > 0; to--) {
val = ATA_INL(ctlr->r_mem, AHCI_PHYCS0R);
if ((val & PHYCS0R_POWER_STATUS_MASK) == PHYCS0R_PS_GOOD)
break;
DELAY(10);
}
if (to == 0 && bootverbose)
device_printf(dev, "PHY Power Failed PHYCS0R = %#x\n", val);
ahci_set(ctlr->r_mem, AHCI_PHYCS2R, PHYCS2R_CALIBRATE);
for (to = PHY_RESET_TIMEOUT; to > 0; to--) {
val = ATA_INL(ctlr->r_mem, AHCI_PHYCS2R);
if ((val & PHYCS2R_CALIBRATE) == 0)
break;
DELAY(10);
}
if (to == 0 && bootverbose)
device_printf(dev, "PHY Cal Failed PHYCS2R %#x\n", val);
DELAY(1000);
ATA_OUTL(ctlr->r_mem, AHCI_RWCR, 7);
}
static void
ahci_a10_ch_start(struct ahci_channel *ch)
{
uint32_t reg;
reg = ATA_INL(ch->r_mem, AHCI_P0DMACR);
reg &= ~0xff00;
reg |= 0x4400;
ATA_OUTL(ch->r_mem, AHCI_P0DMACR, reg);
}
static int
ahci_a10_ctlr_reset(device_t dev)
{
ahci_a10_phy_reset(dev);
return (ahci_ctlr_reset(dev));
}
static int
ahci_a10_probe(device_t dev)
{
if (!ofw_bus_is_compatible(dev, "allwinner,sun4i-a10-ahci"))
return (ENXIO);
device_set_desc(dev, "Allwinner Integrated AHCI controller");
return (BUS_PROBE_DEFAULT);
}
static int
ahci_a10_attach(device_t dev)
{
int error;
struct ahci_a10_softc *sc;
struct ahci_controller *ctlr;
sc = device_get_softc(dev);
ctlr = &sc->ahci_ctlr;
ctlr->quirks = AHCI_Q_NOPMP;
ctlr->vendorid = 0;
ctlr->deviceid = 0;
ctlr->subvendorid = 0;
ctlr->subdeviceid = 0;
ctlr->r_rid = 0;
if (!(ctlr->r_mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
&ctlr->r_rid, RF_ACTIVE)))
return (ENXIO);
if (regulator_get_by_ofw_property(dev, 0, "target-supply",
&sc->ahci_reg) == 0) {
error = regulator_enable(sc->ahci_reg);
if (error != 0) {
device_printf(dev, "Could not enable regulator\n");
goto fail;
}
}
error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk_gate);
if (error != 0) {
device_printf(dev, "Cannot get gate clock\n");
goto fail;
}
error = clk_get_by_ofw_index(dev, 0, 1, &sc->clk_pll);
if (error != 0) {
device_printf(dev, "Cannot get PLL clock\n");
goto fail;
}
error = clk_set_freq(sc->clk_pll, PLL_FREQ, CLK_SET_ROUND_DOWN);
if (error != 0) {
device_printf(dev, "Cannot set PLL frequency\n");
goto fail;
}
error = clk_enable(sc->clk_pll);
if (error != 0) {
device_printf(dev, "Cannot enable PLL\n");
goto fail;
}
error = clk_enable(sc->clk_gate);
if (error != 0) {
device_printf(dev, "Cannot enable clk gate\n");
goto fail;
}
if ((error = ahci_a10_ctlr_reset(dev)) != 0)
goto fail;
ctlr->msi = 0;
ctlr->numirqs = 1;
ctlr->ch_start = ahci_a10_ch_start;
return (ahci_attach(dev));
fail:
if (sc->ahci_reg != NULL)
regulator_disable(sc->ahci_reg);
if (sc->clk_gate != NULL)
clk_release(sc->clk_gate);
if (sc->clk_pll != NULL)
clk_release(sc->clk_pll);
bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
return (error);
}
static int
ahci_a10_detach(device_t dev)
{
struct ahci_a10_softc *sc;
struct ahci_controller *ctlr;
sc = device_get_softc(dev);
ctlr = &sc->ahci_ctlr;
if (sc->ahci_reg != NULL)
regulator_disable(sc->ahci_reg);
if (sc->clk_gate != NULL)
clk_release(sc->clk_gate);
if (sc->clk_pll != NULL)
clk_release(sc->clk_pll);
bus_release_resource(dev, SYS_RES_MEMORY, ctlr->r_rid, ctlr->r_mem);
return (ahci_detach(dev));
}
static device_method_t ahci_ata_methods[] = {
DEVMETHOD(device_probe, ahci_a10_probe),
DEVMETHOD(device_attach, ahci_a10_attach),
DEVMETHOD(device_detach, ahci_a10_detach),
DEVMETHOD(bus_print_child, ahci_print_child),
DEVMETHOD(bus_alloc_resource, ahci_alloc_resource),
DEVMETHOD(bus_release_resource, ahci_release_resource),
DEVMETHOD(bus_setup_intr, ahci_setup_intr),
DEVMETHOD(bus_teardown_intr,ahci_teardown_intr),
DEVMETHOD(bus_child_location, ahci_child_location),
DEVMETHOD_END
};
static driver_t ahci_ata_driver = {
"ahci",
ahci_ata_methods,
sizeof(struct ahci_a10_softc)
};
DRIVER_MODULE(a10_ahci, simplebus, ahci_ata_driver, 0, 0);