Path: blob/master/arch/arm/mach-exynos4/dev-ahci.c
10817 views
/* linux/arch/arm/mach-exynos4/dev-ahci.c1*2* Copyright (c) 2011 Samsung Electronics Co., Ltd.3* http://www.samsung.com4*5* EXYNOS4 - AHCI support6*7* This program is free software; you can redistribute it and/or modify8* it under the terms of the GNU General Public License version 2 as9* published by the Free Software Foundation.10*/1112#include <linux/clk.h>13#include <linux/delay.h>14#include <linux/dma-mapping.h>15#include <linux/platform_device.h>16#include <linux/ahci_platform.h>1718#include <plat/cpu.h>1920#include <mach/irqs.h>21#include <mach/map.h>22#include <mach/regs-pmu.h>2324/* PHY Control Register */25#define SATA_CTRL0 0x026/* PHY Link Control Register */27#define SATA_CTRL1 0x428/* PHY Status Register */29#define SATA_PHY_STATUS 0x83031#define SATA_CTRL0_RX_DATA_VALID(x) (x << 27)32#define SATA_CTRL0_SPEED_MODE (1 << 26)33#define SATA_CTRL0_M_PHY_CAL (1 << 19)34#define SATA_CTRL0_PHY_CMU_RST_N (1 << 10)35#define SATA_CTRL0_M_PHY_LN_RST_N (1 << 9)36#define SATA_CTRL0_PHY_POR_N (1 << 8)3738#define SATA_CTRL1_RST_PMALIVE_N (1 << 8)39#define SATA_CTRL1_RST_RXOOB_N (1 << 7)40#define SATA_CTRL1_RST_RX_N (1 << 6)41#define SATA_CTRL1_RST_TX_N (1 << 5)4243#define SATA_PHY_STATUS_CMU_OK (1 << 18)44#define SATA_PHY_STATUS_LANE_OK (1 << 16)4546#define LANE0 0x20047#define COM_LANE 0xA004849#define HOST_PORTS_IMPL 0xC50#define SCLK_SATA_FREQ (67 * MHZ)5152static void __iomem *phy_base, *phy_ctrl;5354struct phy_reg {55u8 reg;56u8 val;57};5859/* SATA PHY setup */60static const struct phy_reg exynos4_sataphy_cmu[] = {61{ 0x00, 0x06 }, { 0x02, 0x80 }, { 0x22, 0xa0 }, { 0x23, 0x42 },62{ 0x2e, 0x04 }, { 0x2f, 0x50 }, { 0x30, 0x70 }, { 0x31, 0x02 },63{ 0x32, 0x25 }, { 0x33, 0x40 }, { 0x34, 0x01 }, { 0x35, 0x40 },64{ 0x61, 0x2e }, { 0x63, 0x5e }, { 0x65, 0x42 }, { 0x66, 0xd1 },65{ 0x67, 0x20 }, { 0x68, 0x28 }, { 0x69, 0x78 }, { 0x6a, 0x04 },66{ 0x6b, 0xc8 }, { 0x6c, 0x06 },67};6869static const struct phy_reg exynos4_sataphy_lane[] = {70{ 0x00, 0x02 }, { 0x05, 0x10 }, { 0x06, 0x84 }, { 0x07, 0x04 },71{ 0x08, 0xe0 }, { 0x10, 0x23 }, { 0x13, 0x05 }, { 0x14, 0x30 },72{ 0x15, 0x00 }, { 0x17, 0x70 }, { 0x18, 0xf2 }, { 0x19, 0x1e },73{ 0x1a, 0x18 }, { 0x1b, 0x0d }, { 0x1c, 0x08 }, { 0x50, 0x60 },74{ 0x51, 0x0f },75};7677static const struct phy_reg exynos4_sataphy_comlane[] = {78{ 0x01, 0x20 }, { 0x03, 0x40 }, { 0x04, 0x3c }, { 0x05, 0x7d },79{ 0x06, 0x1d }, { 0x07, 0xcf }, { 0x08, 0x05 }, { 0x09, 0x63 },80{ 0x0a, 0x29 }, { 0x0b, 0xc4 }, { 0x0c, 0x01 }, { 0x0d, 0x03 },81{ 0x0e, 0x28 }, { 0x0f, 0x98 }, { 0x10, 0x19 }, { 0x13, 0x80 },82{ 0x14, 0xf0 }, { 0x15, 0xd0 }, { 0x39, 0xa0 }, { 0x3a, 0xa0 },83{ 0x3b, 0xa0 }, { 0x3c, 0xa0 }, { 0x3d, 0xa0 }, { 0x3e, 0xa0 },84{ 0x3f, 0xa0 }, { 0x40, 0x42 }, { 0x42, 0x80 }, { 0x43, 0x58 },85{ 0x45, 0x44 }, { 0x46, 0x5c }, { 0x47, 0x86 }, { 0x48, 0x8d },86{ 0x49, 0xd0 }, { 0x4a, 0x09 }, { 0x4b, 0x90 }, { 0x4c, 0x07 },87{ 0x4d, 0x40 }, { 0x51, 0x20 }, { 0x52, 0x32 }, { 0x7f, 0xd8 },88{ 0x80, 0x1a }, { 0x81, 0xff }, { 0x82, 0x11 }, { 0x83, 0x00 },89{ 0x87, 0xf0 }, { 0x87, 0xff }, { 0x87, 0xff }, { 0x87, 0xff },90{ 0x87, 0xff }, { 0x8c, 0x1c }, { 0x8d, 0xc2 }, { 0x8e, 0xc3 },91{ 0x8f, 0x3f }, { 0x90, 0x0a }, { 0x96, 0xf8 },92};9394static int wait_for_phy_ready(void __iomem *reg, unsigned long bit)95{96unsigned long timeout;9798/* wait for maximum of 3 sec */99timeout = jiffies + msecs_to_jiffies(3000);100while (!(__raw_readl(reg) & bit)) {101if (time_after(jiffies, timeout))102return -1;103cpu_relax();104}105return 0;106}107108static int ahci_phy_init(void __iomem *mmio)109{110int i, ctrl0;111112for (i = 0; i < ARRAY_SIZE(exynos4_sataphy_cmu); i++)113__raw_writeb(exynos4_sataphy_cmu[i].val,114phy_base + (exynos4_sataphy_cmu[i].reg * 4));115116for (i = 0; i < ARRAY_SIZE(exynos4_sataphy_lane); i++)117__raw_writeb(exynos4_sataphy_lane[i].val,118phy_base + (LANE0 + exynos4_sataphy_lane[i].reg) * 4);119120for (i = 0; i < ARRAY_SIZE(exynos4_sataphy_comlane); i++)121__raw_writeb(exynos4_sataphy_comlane[i].val,122phy_base + (COM_LANE + exynos4_sataphy_comlane[i].reg) * 4);123124__raw_writeb(0x07, phy_base);125126ctrl0 = __raw_readl(phy_ctrl + SATA_CTRL0);127ctrl0 |= SATA_CTRL0_PHY_CMU_RST_N;128__raw_writel(ctrl0, phy_ctrl + SATA_CTRL0);129130if (wait_for_phy_ready(phy_ctrl + SATA_PHY_STATUS,131SATA_PHY_STATUS_CMU_OK) < 0) {132printk(KERN_ERR "PHY CMU not ready\n");133return -EBUSY;134}135136__raw_writeb(0x03, phy_base + (COM_LANE * 4));137138ctrl0 = __raw_readl(phy_ctrl + SATA_CTRL0);139ctrl0 |= SATA_CTRL0_M_PHY_LN_RST_N;140__raw_writel(ctrl0, phy_ctrl + SATA_CTRL0);141142if (wait_for_phy_ready(phy_ctrl + SATA_PHY_STATUS,143SATA_PHY_STATUS_LANE_OK) < 0) {144printk(KERN_ERR "PHY LANE not ready\n");145return -EBUSY;146}147148ctrl0 = __raw_readl(phy_ctrl + SATA_CTRL0);149ctrl0 |= SATA_CTRL0_M_PHY_CAL;150__raw_writel(ctrl0, phy_ctrl + SATA_CTRL0);151152return 0;153}154155static int exynos4_ahci_init(struct device *dev, void __iomem *mmio)156{157struct clk *clk_sata, *clk_sataphy, *clk_sclk_sata;158int val, ret;159160phy_base = ioremap(EXYNOS4_PA_SATAPHY, SZ_64K);161if (!phy_base) {162dev_err(dev, "failed to allocate memory for SATA PHY\n");163return -ENOMEM;164}165166phy_ctrl = ioremap(EXYNOS4_PA_SATAPHY_CTRL, SZ_16);167if (!phy_ctrl) {168dev_err(dev, "failed to allocate memory for SATA PHY CTRL\n");169ret = -ENOMEM;170goto err1;171}172173clk_sata = clk_get(dev, "sata");174if (IS_ERR(clk_sata)) {175dev_err(dev, "failed to get sata clock\n");176ret = PTR_ERR(clk_sata);177clk_sata = NULL;178goto err2;179180}181clk_enable(clk_sata);182183clk_sataphy = clk_get(dev, "sataphy");184if (IS_ERR(clk_sataphy)) {185dev_err(dev, "failed to get sataphy clock\n");186ret = PTR_ERR(clk_sataphy);187clk_sataphy = NULL;188goto err3;189}190clk_enable(clk_sataphy);191192clk_sclk_sata = clk_get(dev, "sclk_sata");193if (IS_ERR(clk_sclk_sata)) {194dev_err(dev, "failed to get sclk_sata\n");195ret = PTR_ERR(clk_sclk_sata);196clk_sclk_sata = NULL;197goto err4;198}199clk_enable(clk_sclk_sata);200clk_set_rate(clk_sclk_sata, SCLK_SATA_FREQ);201202__raw_writel(S5P_PMU_SATA_PHY_CONTROL_EN, S5P_PMU_SATA_PHY_CONTROL);203204/* Enable PHY link control */205val = SATA_CTRL1_RST_PMALIVE_N | SATA_CTRL1_RST_RXOOB_N |206SATA_CTRL1_RST_RX_N | SATA_CTRL1_RST_TX_N;207__raw_writel(val, phy_ctrl + SATA_CTRL1);208209/* Set communication speed as 3Gbps and enable PHY power */210val = SATA_CTRL0_RX_DATA_VALID(3) | SATA_CTRL0_SPEED_MODE |211SATA_CTRL0_PHY_POR_N;212__raw_writel(val, phy_ctrl + SATA_CTRL0);213214/* Port0 is available */215__raw_writel(0x1, mmio + HOST_PORTS_IMPL);216217return ahci_phy_init(mmio);218219err4:220clk_disable(clk_sataphy);221clk_put(clk_sataphy);222err3:223clk_disable(clk_sata);224clk_put(clk_sata);225err2:226iounmap(phy_ctrl);227err1:228iounmap(phy_base);229230return ret;231}232233static struct ahci_platform_data exynos4_ahci_pdata = {234.init = exynos4_ahci_init,235};236237static struct resource exynos4_ahci_resource[] = {238[0] = {239.start = EXYNOS4_PA_SATA,240.end = EXYNOS4_PA_SATA + SZ_64K - 1,241.flags = IORESOURCE_MEM,242},243[1] = {244.start = IRQ_SATA,245.end = IRQ_SATA,246.flags = IORESOURCE_IRQ,247},248};249250static u64 exynos4_ahci_dmamask = DMA_BIT_MASK(32);251252struct platform_device exynos4_device_ahci = {253.name = "ahci",254.id = -1,255.resource = exynos4_ahci_resource,256.num_resources = ARRAY_SIZE(exynos4_ahci_resource),257.dev = {258.platform_data = &exynos4_ahci_pdata,259.dma_mask = &exynos4_ahci_dmamask,260.coherent_dma_mask = DMA_BIT_MASK(32),261},262};263264265