blob: cfd9eae38203841ab15af5a5d0b87dd621510e67 [file] [log] [blame]
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Joel Nider5556a852011-10-16 10:52:13 +02002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
Joel Nider5bd73f82011-12-14 16:53:30 +020013#include <linux/module.h> /* Just for modules */
14#include <linux/kernel.h> /* Only for KERN_INFO */
15#include <linux/err.h> /* Error macros */
16#include <linux/list.h> /* Linked list */
Joel Nider5556a852011-10-16 10:52:13 +020017#include <linux/cdev.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020018#include <linux/init.h> /* Needed for the macros */
19#include <linux/io.h> /* IO macros */
20#include <linux/device.h> /* Device drivers need this */
21#include <linux/sched.h> /* Externally defined globals */
22#include <linux/pm_runtime.h> /* Runtime power management */
Joel Nider5556a852011-10-16 10:52:13 +020023#include <linux/fs.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020024#include <linux/uaccess.h> /* copy_to_user */
Joel Nider5556a852011-10-16 10:52:13 +020025#include <linux/slab.h> /* kfree, kzalloc */
Joel Nider5bd73f82011-12-14 16:53:30 +020026#include <linux/ioport.h> /* XXX_ mem_region */
27#include <linux/dma-mapping.h> /* dma_XXX */
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +020028#include <linux/dmapool.h> /* DMA pools */
Joel Nider5bd73f82011-12-14 16:53:30 +020029#include <linux/delay.h> /* msleep */
30#include <linux/platform_device.h>
Joel Nider5556a852011-10-16 10:52:13 +020031#include <linux/clk.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020032#include <linux/poll.h> /* poll() file op */
33#include <linux/wait.h> /* wait() macros, sleeping */
34#include <linux/tspp.h> /* tspp functions */
Joel Nider5556a852011-10-16 10:52:13 +020035#include <linux/bitops.h> /* BIT() macro */
Joel Nider5bd73f82011-12-14 16:53:30 +020036#include <mach/sps.h> /* BAM stuff */
Joel Nider5556a852011-10-16 10:52:13 +020037#include <mach/gpio.h>
Joel Nider5bd73f82011-12-14 16:53:30 +020038#include <linux/wakelock.h> /* Locking functions */
Hamad Kadmany567bed82012-11-29 14:15:57 +020039#include <linux/timer.h> /* Timer services */
40#include <linux/jiffies.h> /* Jiffies counter */
Joel Nider5556a852011-10-16 10:52:13 +020041#include <mach/dma.h>
42#include <mach/msm_tspp.h>
Joel Nider5556a852011-10-16 10:52:13 +020043#include <linux/debugfs.h>
Liron Kuch8fa85b02013-01-01 18:29:47 +020044#include <linux/of.h>
45#include <linux/of_gpio.h>
Joel Nider5556a852011-10-16 10:52:13 +020046
47/*
48 * General defines
49 */
Joel Nider5556a852011-10-16 10:52:13 +020050#define TSPP_TSIF_INSTANCES 2
51#define TSPP_FILTER_TABLES 3
Joel Nider5bd73f82011-12-14 16:53:30 +020052#define TSPP_MAX_DEVICES 1
Joel Nider5556a852011-10-16 10:52:13 +020053#define TSPP_NUM_CHANNELS 16
54#define TSPP_NUM_PRIORITIES 16
55#define TSPP_NUM_KEYS 8
56#define INVALID_CHANNEL 0xFFFFFFFF
Hamad Kadmany567bed82012-11-29 14:15:57 +020057
58/*
59 * BAM descriptor FIFO size (in number of descriptors).
60 * Max number of descriptors allowed by SPS which is 8K-1.
61 * Restrict it to half of this to save DMA memory.
62 */
63#define TSPP_SPS_DESCRIPTOR_COUNT (4 * 1024 - 1)
Joel Nider5556a852011-10-16 10:52:13 +020064#define TSPP_PACKET_LENGTH 188
65#define TSPP_MIN_BUFFER_SIZE (TSPP_PACKET_LENGTH)
Hamad Kadmany567bed82012-11-29 14:15:57 +020066
67/* Max descriptor buffer size allowed by SPS */
68#define TSPP_MAX_BUFFER_SIZE (32 * 1024 - 1)
69
70/*
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +020071 * Returns whether to use DMA pool for TSPP output buffers.
72 * For buffers smaller than page size, using DMA pool
73 * provides better memory utilization as dma_alloc_coherent
74 * allocates minimum of page size.
75 */
76#define TSPP_USE_DMA_POOL(buff_size) ((buff_size) < PAGE_SIZE)
77
78/*
Hamad Kadmany567bed82012-11-29 14:15:57 +020079 * Max allowed TSPP buffers/descriptors.
80 * If SPS desc FIFO holds X descriptors, we can queue up to X-1 descriptors.
81 */
82#define TSPP_NUM_BUFFERS (TSPP_SPS_DESCRIPTOR_COUNT - 1)
Joel Nider5556a852011-10-16 10:52:13 +020083#define TSPP_TSIF_DEFAULT_TIME_LIMIT 60
84#define SPS_DESCRIPTOR_SIZE 8
85#define MIN_ACCEPTABLE_BUFFER_COUNT 2
Joel Nider5bd73f82011-12-14 16:53:30 +020086#define TSPP_DEBUG(msg...)
Joel Nider5556a852011-10-16 10:52:13 +020087
88/*
89 * TSIF register offsets
90 */
91#define TSIF_STS_CTL_OFF (0x0)
92#define TSIF_TIME_LIMIT_OFF (0x4)
93#define TSIF_CLK_REF_OFF (0x8)
94#define TSIF_LPBK_FLAGS_OFF (0xc)
95#define TSIF_LPBK_DATA_OFF (0x10)
96#define TSIF_TEST_CTL_OFF (0x14)
97#define TSIF_TEST_MODE_OFF (0x18)
98#define TSIF_TEST_RESET_OFF (0x1c)
99#define TSIF_TEST_EXPORT_OFF (0x20)
100#define TSIF_TEST_CURRENT_OFF (0x24)
101
102#define TSIF_DATA_PORT_OFF (0x100)
103
104/* bits for TSIF_STS_CTL register */
105#define TSIF_STS_CTL_EN_IRQ BIT(28)
106#define TSIF_STS_CTL_PACK_AVAIL BIT(27)
107#define TSIF_STS_CTL_1ST_PACKET BIT(26)
108#define TSIF_STS_CTL_OVERFLOW BIT(25)
109#define TSIF_STS_CTL_LOST_SYNC BIT(24)
110#define TSIF_STS_CTL_TIMEOUT BIT(23)
111#define TSIF_STS_CTL_INV_SYNC BIT(21)
112#define TSIF_STS_CTL_INV_NULL BIT(20)
113#define TSIF_STS_CTL_INV_ERROR BIT(19)
114#define TSIF_STS_CTL_INV_ENABLE BIT(18)
115#define TSIF_STS_CTL_INV_DATA BIT(17)
116#define TSIF_STS_CTL_INV_CLOCK BIT(16)
117#define TSIF_STS_CTL_SPARE BIT(15)
118#define TSIF_STS_CTL_EN_NULL BIT(11)
119#define TSIF_STS_CTL_EN_ERROR BIT(10)
120#define TSIF_STS_CTL_LAST_BIT BIT(9)
121#define TSIF_STS_CTL_EN_TIME_LIM BIT(8)
122#define TSIF_STS_CTL_EN_TCR BIT(7)
123#define TSIF_STS_CTL_TEST_MODE BIT(6)
Joel Nider5bd73f82011-12-14 16:53:30 +0200124#define TSIF_STS_CTL_MODE_2 BIT(5)
Joel Nider5556a852011-10-16 10:52:13 +0200125#define TSIF_STS_CTL_EN_DM BIT(4)
126#define TSIF_STS_CTL_STOP BIT(3)
127#define TSIF_STS_CTL_START BIT(0)
128
129/*
130 * TSPP register offsets
131 */
Liron Kuch229090d2012-10-30 17:47:50 +0200132#define TSPP_RST 0x00
Joel Nider5556a852011-10-16 10:52:13 +0200133#define TSPP_CLK_CONTROL 0x04
Liron Kuch229090d2012-10-30 17:47:50 +0200134#define TSPP_CONFIG 0x08
135#define TSPP_CONTROL 0x0C
Joel Nider5556a852011-10-16 10:52:13 +0200136#define TSPP_PS_DISABLE 0x10
Liron Kuch229090d2012-10-30 17:47:50 +0200137#define TSPP_MSG_IRQ_STATUS 0x14
Joel Nider5556a852011-10-16 10:52:13 +0200138#define TSPP_MSG_IRQ_MASK 0x18
139#define TSPP_IRQ_STATUS 0x1C
140#define TSPP_IRQ_MASK 0x20
141#define TSPP_IRQ_CLEAR 0x24
142#define TSPP_PIPE_ERROR_STATUS(_n) (0x28 + (_n << 2))
Liron Kuch229090d2012-10-30 17:47:50 +0200143#define TSPP_STATUS 0x68
144#define TSPP_CURR_TSP_HEADER 0x6C
145#define TSPP_CURR_PID_FILTER 0x70
146#define TSPP_SYSTEM_KEY(_n) (0x74 + (_n << 2))
147#define TSPP_CBC_INIT_VAL(_n) (0x94 + (_n << 2))
148#define TSPP_DATA_KEY_RESET 0x9C
Joel Nider5556a852011-10-16 10:52:13 +0200149#define TSPP_KEY_VALID 0xA0
150#define TSPP_KEY_ERROR 0xA4
151#define TSPP_TEST_CTRL 0xA8
Liron Kuch229090d2012-10-30 17:47:50 +0200152#define TSPP_VERSION 0xAC
Joel Nider5556a852011-10-16 10:52:13 +0200153#define TSPP_GENERICS 0xB0
Liron Kuch229090d2012-10-30 17:47:50 +0200154#define TSPP_NOP 0xB4
Joel Nider5556a852011-10-16 10:52:13 +0200155
156/*
157 * Register bit definitions
158 */
159/* TSPP_RST */
160#define TSPP_RST_RESET BIT(0)
161
162/* TSPP_CLK_CONTROL */
163#define TSPP_CLK_CONTROL_FORCE_CRYPTO BIT(9)
164#define TSPP_CLK_CONTROL_FORCE_PES_PL BIT(8)
165#define TSPP_CLK_CONTROL_FORCE_PES_AF BIT(7)
166#define TSPP_CLK_CONTROL_FORCE_RAW_CTRL BIT(6)
167#define TSPP_CLK_CONTROL_FORCE_PERF_CNT BIT(5)
168#define TSPP_CLK_CONTROL_FORCE_CTX_SEARCH BIT(4)
169#define TSPP_CLK_CONTROL_FORCE_TSP_PROC BIT(3)
170#define TSPP_CLK_CONTROL_FORCE_CONS_AHB2MEM BIT(2)
171#define TSPP_CLK_CONTROL_FORCE_TS_AHB2MEM BIT(1)
172#define TSPP_CLK_CONTROL_SET_CLKON BIT(0)
173
174/* TSPP_CONFIG */
175#define TSPP_CONFIG_SET_PACKET_LENGTH(_a, _b) (_a = (_a & 0xF0) | \
176((_b & 0xF) << 8))
177#define TSPP_CONFIG_GET_PACKET_LENGTH(_a) ((_a >> 8) & 0xF)
178#define TSPP_CONFIG_DUP_WITH_DISC_EN BIT(7)
179#define TSPP_CONFIG_PES_SYNC_ERROR_MASK BIT(6)
180#define TSPP_CONFIG_PS_LEN_ERR_MASK BIT(5)
181#define TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK BIT(4)
182#define TSPP_CONFIG_PS_CONT_ERR_MASK BIT(3)
183#define TSPP_CONFIG_PS_DUP_TSP_MASK BIT(2)
184#define TSPP_CONFIG_TSP_ERR_IND_MASK BIT(1)
185#define TSPP_CONFIG_TSP_SYNC_ERR_MASK BIT(0)
186
187/* TSPP_CONTROL */
188#define TSPP_CONTROL_PID_FILTER_LOCK BIT(5)
189#define TSPP_CONTROL_FORCE_KEY_CALC BIT(4)
190#define TSPP_CONTROL_TSP_CONS_SRC_DIS BIT(3)
191#define TSPP_CONTROL_TSP_TSIF1_SRC_DIS BIT(2)
192#define TSPP_CONTROL_TSP_TSIF0_SRC_DIS BIT(1)
193#define TSPP_CONTROL_PERF_COUNT_INIT BIT(0)
194
195/* TSPP_MSG_IRQ_STATUS + TSPP_MSG_IRQ_MASK */
196#define TSPP_MSG_TSPP_IRQ BIT(2)
197#define TSPP_MSG_TSIF_1_IRQ BIT(1)
198#define TSPP_MSG_TSIF_0_IRQ BIT(0)
199
200/* TSPP_IRQ_STATUS + TSPP_IRQ_MASK + TSPP_IRQ_CLEAR */
Liron Kuch229090d2012-10-30 17:47:50 +0200201#define TSPP_IRQ_STATUS_TSP_RD_CMPL BIT(19)
202#define TSPP_IRQ_STATUS_KEY_ERROR BIT(18)
Joel Nider5556a852011-10-16 10:52:13 +0200203#define TSPP_IRQ_STATUS_KEY_SWITCHED_BAD BIT(17)
204#define TSPP_IRQ_STATUS_KEY_SWITCHED BIT(16)
205#define TSPP_IRQ_STATUS_PS_BROKEN(_n) BIT((_n))
206
207/* TSPP_PIPE_ERROR_STATUS */
Liron Kuch229090d2012-10-30 17:47:50 +0200208#define TSPP_PIPE_PES_SYNC_ERROR BIT(3)
209#define TSPP_PIPE_PS_LENGTH_ERROR BIT(2)
Joel Nider5556a852011-10-16 10:52:13 +0200210#define TSPP_PIPE_PS_CONTINUITY_ERROR BIT(1)
Liron Kuch229090d2012-10-30 17:47:50 +0200211#define TSPP_PIP_PS_LOST_START BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200212
213/* TSPP_STATUS */
Liron Kuch229090d2012-10-30 17:47:50 +0200214#define TSPP_STATUS_TSP_PKT_AVAIL BIT(10)
215#define TSPP_STATUS_TSIF1_DM_REQ BIT(6)
216#define TSPP_STATUS_TSIF0_DM_REQ BIT(2)
217#define TSPP_CURR_FILTER_TABLE BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200218
219/* TSPP_GENERICS */
Liron Kuch229090d2012-10-30 17:47:50 +0200220#define TSPP_GENERICS_CRYPTO_GEN BIT(12)
Joel Nider5556a852011-10-16 10:52:13 +0200221#define TSPP_GENERICS_MAX_CONS_PIPES BIT(7)
Liron Kuch229090d2012-10-30 17:47:50 +0200222#define TSPP_GENERICS_MAX_PIPES BIT(2)
223#define TSPP_GENERICS_TSIF_1_GEN BIT(1)
224#define TSPP_GENERICS_TSIF_0_GEN BIT(0)
Joel Nider5556a852011-10-16 10:52:13 +0200225
226/*
227 * TSPP memory regions
228 */
229#define TSPP_PID_FILTER_TABLE0 0x800
230#define TSPP_PID_FILTER_TABLE1 0x880
231#define TSPP_PID_FILTER_TABLE2 0x900
232#define TSPP_GLOBAL_PERFORMANCE 0x980 /* see tspp_global_performance */
233#define TSPP_PIPE_CONTEXT 0x990 /* see tspp_pipe_context */
234#define TSPP_PIPE_PERFORMANCE 0x998 /* see tspp_pipe_performance */
235#define TSPP_TSP_BUFF_WORD(_n) (0xC10 + (_n << 2))
236#define TSPP_DATA_KEY 0xCD0
237
Joel Nider5556a852011-10-16 10:52:13 +0200238struct debugfs_entry {
239 const char *name;
240 mode_t mode;
241 int offset;
242};
243
244static const struct debugfs_entry debugfs_tsif_regs[] = {
245 {"sts_ctl", S_IRUGO | S_IWUSR, TSIF_STS_CTL_OFF},
246 {"time_limit", S_IRUGO | S_IWUSR, TSIF_TIME_LIMIT_OFF},
247 {"clk_ref", S_IRUGO | S_IWUSR, TSIF_CLK_REF_OFF},
248 {"lpbk_flags", S_IRUGO | S_IWUSR, TSIF_LPBK_FLAGS_OFF},
249 {"lpbk_data", S_IRUGO | S_IWUSR, TSIF_LPBK_DATA_OFF},
250 {"test_ctl", S_IRUGO | S_IWUSR, TSIF_TEST_CTL_OFF},
251 {"test_mode", S_IRUGO | S_IWUSR, TSIF_TEST_MODE_OFF},
252 {"test_reset", S_IWUSR, TSIF_TEST_RESET_OFF},
253 {"test_export", S_IRUGO | S_IWUSR, TSIF_TEST_EXPORT_OFF},
254 {"test_current", S_IRUGO, TSIF_TEST_CURRENT_OFF},
255 {"data_port", S_IRUSR, TSIF_DATA_PORT_OFF},
256};
257
258static const struct debugfs_entry debugfs_tspp_regs[] = {
259 {"rst", S_IRUGO | S_IWUSR, TSPP_RST},
260 {"clk_control", S_IRUGO | S_IWUSR, TSPP_CLK_CONTROL},
261 {"config", S_IRUGO | S_IWUSR, TSPP_CONFIG},
262 {"control", S_IRUGO | S_IWUSR, TSPP_CONTROL},
263 {"ps_disable", S_IRUGO | S_IWUSR, TSPP_PS_DISABLE},
264 {"msg_irq_status", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_STATUS},
265 {"msg_irq_mask", S_IRUGO | S_IWUSR, TSPP_MSG_IRQ_MASK},
266 {"irq_status", S_IRUGO | S_IWUSR, TSPP_IRQ_STATUS},
267 {"irq_mask", S_IRUGO | S_IWUSR, TSPP_IRQ_MASK},
268 {"irq_clear", S_IRUGO | S_IWUSR, TSPP_IRQ_CLEAR},
269 /* {"pipe_error_status",S_IRUGO | S_IWUSR, TSPP_PIPE_ERROR_STATUS}, */
270 {"status", S_IRUGO | S_IWUSR, TSPP_STATUS},
271 {"curr_tsp_header", S_IRUGO | S_IWUSR, TSPP_CURR_TSP_HEADER},
272 {"curr_pid_filter", S_IRUGO | S_IWUSR, TSPP_CURR_PID_FILTER},
273 /* {"system_key", S_IRUGO | S_IWUSR, TSPP_SYSTEM_KEY}, */
274 /* {"cbc_init_val", S_IRUGO | S_IWUSR, TSPP_CBC_INIT_VAL}, */
275 {"data_key_reset", S_IRUGO | S_IWUSR, TSPP_DATA_KEY_RESET},
276 {"key_valid", S_IRUGO | S_IWUSR, TSPP_KEY_VALID},
277 {"key_error", S_IRUGO | S_IWUSR, TSPP_KEY_ERROR},
278 {"test_ctrl", S_IRUGO | S_IWUSR, TSPP_TEST_CTRL},
279 {"version", S_IRUGO | S_IWUSR, TSPP_VERSION},
280 {"generics", S_IRUGO | S_IWUSR, TSPP_GENERICS},
281 {"pid_filter_table0", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE0},
282 {"pid_filter_table1", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE1},
283 {"pid_filter_table2", S_IRUGO | S_IWUSR, TSPP_PID_FILTER_TABLE2},
284 {"global_performance", S_IRUGO | S_IWUSR, TSPP_GLOBAL_PERFORMANCE},
285 {"pipe_context", S_IRUGO | S_IWUSR, TSPP_PIPE_CONTEXT},
286 {"pipe_performance", S_IRUGO | S_IWUSR, TSPP_PIPE_PERFORMANCE},
287 {"data_key", S_IRUGO | S_IWUSR, TSPP_DATA_KEY}
288};
289
Joel Nider5556a852011-10-16 10:52:13 +0200290struct tspp_pid_filter {
291 u32 filter; /* see FILTER_ macros */
292 u32 config; /* see FILTER_ macros */
293};
294
295/* tsp_info */
296#define FILTER_HEADER_ERROR_MASK BIT(7)
297#define FILTER_TRANS_END_DISABLE BIT(6)
298#define FILTER_DEC_ON_ERROR_EN BIT(5)
299#define FILTER_DECRYPT BIT(4)
300#define FILTER_HAS_ENCRYPTION(_p) (_p->config & FILTER_DECRYPT)
301#define FILTER_GET_PIPE_NUMBER0(_p) (_p->config & 0xF)
302#define FILTER_SET_PIPE_NUMBER0(_p, _b) (_p->config = \
303 (_p->config & ~0xF) | (_b & 0xF))
304#define FILTER_GET_PIPE_PROCESS0(_p) ((_p->filter >> 30) & 0x3)
305#define FILTER_SET_PIPE_PROCESS0(_p, _b) (_p->filter = \
306 (_p->filter & ~(0x3<<30)) | ((_b & 0x3) << 30))
307#define FILTER_GET_PIPE_PID(_p) ((_p->filter >> 13) & 0x1FFF)
308#define FILTER_SET_PIPE_PID(_p, _b) (_p->filter = \
309 (_p->filter & ~(0x1FFF<<13)) | ((_b & 0x1FFF) << 13))
310#define FILTER_GET_PID_MASK(_p) (_p->filter & 0x1FFF)
311#define FILTER_SET_PID_MASK(_p, _b) (_p->filter = \
312 (_p->filter & ~0x1FFF) | (_b & 0x1FFF))
313#define FILTER_GET_PIPE_PROCESS1(_p) ((_p->config >> 30) & 0x3)
314#define FILTER_SET_PIPE_PROCESS1(_p, _b) (_p->config = \
315 (_p->config & ~(0x3<<30)) | ((_b & 0x3) << 30))
316#define FILTER_GET_KEY_NUMBER(_p) ((_p->config >> 8) & 0x7)
317#define FILTER_SET_KEY_NUMBER(_p, _b) (_p->config = \
318 (_p->config & ~(0x7<<8)) | ((_b & 0x7) << 8))
319
320struct tspp_global_performance_regs {
321 u32 tsp_total;
322 u32 tsp_ignored;
323 u32 tsp_error;
324 u32 tsp_sync;
325};
326
327struct tspp_pipe_context_regs {
328 u16 pes_bytes_left;
329 u16 count;
330 u32 tsif_suffix;
331} __packed;
332#define CONTEXT_GET_STATE(_a) (_a & 0x3)
333#define CONTEXT_UNSPEC_LENGTH BIT(11)
334#define CONTEXT_GET_CONT_COUNT(_a) ((_a >> 12) & 0xF)
335
Hamad Kadmany567bed82012-11-29 14:15:57 +0200336#define MSEC_TO_JIFFIES(msec) ((msec) * HZ / 1000)
337
Joel Nider5556a852011-10-16 10:52:13 +0200338struct tspp_pipe_performance_regs {
339 u32 tsp_total;
340 u32 ps_duplicate_tsp;
341 u32 tsp_no_payload;
342 u32 tsp_broken_ps;
343 u32 ps_total_num;
344 u32 ps_continuity_error;
345 u32 ps_length_error;
346 u32 pes_sync_error;
347};
348
349struct tspp_tsif_device {
350 void __iomem *base;
351 u32 time_limit;
352 u32 ref_count;
Joel Nider5bd73f82011-12-14 16:53:30 +0200353 enum tspp_tsif_mode mode;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200354 int clock_inverse;
355 int data_inverse;
356 int sync_inverse;
357 int enable_inverse;
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200358 u32 tsif_irq;
Joel Nider5556a852011-10-16 10:52:13 +0200359
360 /* debugfs */
Joel Nider5556a852011-10-16 10:52:13 +0200361 struct dentry *dent_tsif;
362 struct dentry *debugfs_tsif_regs[ARRAY_SIZE(debugfs_tsif_regs)];
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200363 u32 stat_rx;
364 u32 stat_overflow;
365 u32 stat_lost_sync;
366 u32 stat_timeout;
Joel Nider5556a852011-10-16 10:52:13 +0200367};
368
369enum tspp_buf_state {
370 TSPP_BUF_STATE_EMPTY, /* buffer has been allocated, but not waiting */
371 TSPP_BUF_STATE_WAITING, /* buffer is waiting to be filled */
Joel Nider5bd73f82011-12-14 16:53:30 +0200372 TSPP_BUF_STATE_DATA, /* buffer is not empty and can be read */
373 TSPP_BUF_STATE_LOCKED /* buffer is being read by a client */
Joel Nider5556a852011-10-16 10:52:13 +0200374};
375
376struct tspp_mem_buffer {
Joel Nider5bd73f82011-12-14 16:53:30 +0200377 struct tspp_mem_buffer *next;
378 struct sps_mem_buffer sps;
379 struct tspp_data_descriptor desc; /* buffer descriptor for kernel api */
Joel Nider5556a852011-10-16 10:52:13 +0200380 enum tspp_buf_state state;
381 size_t filled; /* how much data this buffer is holding */
382 int read_index; /* where to start reading data from */
383};
384
385/* this represents each char device 'channel' */
386struct tspp_channel {
387 struct cdev cdev;
388 struct device *dd;
Joel Nider5bd73f82011-12-14 16:53:30 +0200389 struct tspp_device *pdev; /* can use container_of instead? */
Joel Nider5556a852011-10-16 10:52:13 +0200390 struct sps_pipe *pipe;
391 struct sps_connect config;
392 struct sps_register_event event;
Joel Nider5bd73f82011-12-14 16:53:30 +0200393 struct tspp_mem_buffer *data; /* list of buffers */
394 struct tspp_mem_buffer *read; /* first buffer ready to be read */
395 struct tspp_mem_buffer *waiting; /* first outstanding transfer */
396 struct tspp_mem_buffer *locked; /* buffer currently being read */
Joel Nider5556a852011-10-16 10:52:13 +0200397 wait_queue_head_t in_queue; /* set when data is received */
Joel Nider5bd73f82011-12-14 16:53:30 +0200398 u32 id; /* channel id (0-15) */
399 int used; /* is this channel in use? */
400 int key; /* which encryption key index is used */
401 u32 buffer_size; /* size of the sps transfer buffers */
402 u32 max_buffers; /* how many buffers should be allocated */
403 u32 buffer_count; /* how many buffers are actually allocated */
404 u32 filter_count; /* how many filters have been added to this channel */
405 u32 int_freq; /* generate interrupts every x descriptors */
Joel Nider5556a852011-10-16 10:52:13 +0200406 enum tspp_source src;
407 enum tspp_mode mode;
Joel Nider5bd73f82011-12-14 16:53:30 +0200408 tspp_notifier *notifier; /* used only with kernel api */
409 void *notify_data; /* data to be passed with the notifier */
Hamad Kadmany567bed82012-11-29 14:15:57 +0200410 u32 expiration_period_ms; /* notification on partially filled buffers */
411 struct timer_list expiration_timer;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200412 struct dma_pool *dma_pool;
Liron Kuch229090d2012-10-30 17:47:50 +0200413 tspp_memfree *memfree; /* user defined memory free function */
414 void *user_info; /* user cookie passed to memory alloc/free function */
Joel Nider5556a852011-10-16 10:52:13 +0200415};
416
417struct tspp_pid_filter_table {
418 struct tspp_pid_filter filter[TSPP_NUM_PRIORITIES];
419};
420
421struct tspp_key_entry {
422 u32 even_lsb;
423 u32 even_msb;
424 u32 odd_lsb;
425 u32 odd_msb;
426};
427
428struct tspp_key_table {
429 struct tspp_key_entry entry[TSPP_NUM_KEYS];
430};
431
Joel Nider5bd73f82011-12-14 16:53:30 +0200432/* this represents the actual hardware device */
433struct tspp_device {
434 struct list_head devlist; /* list of all devices */
435 struct platform_device *pdev;
436 void __iomem *base;
437 unsigned int tspp_irq;
438 unsigned int bam_irq;
439 u32 bam_handle;
440 struct sps_bam_props bam_props;
441 struct wake_lock wake_lock;
442 spinlock_t spinlock;
443 struct tasklet_struct tlet;
444 struct tspp_tsif_device tsif[TSPP_TSIF_INSTANCES];
445 /* clocks */
446 struct clk *tsif_pclk;
447 struct clk *tsif_ref_clk;
448 /* data */
449 struct tspp_pid_filter_table *filters[TSPP_FILTER_TABLES];
450 struct tspp_channel channels[TSPP_NUM_CHANNELS];
451 struct tspp_key_table *tspp_key_table;
452 struct tspp_global_performance_regs *tspp_global_performance;
453 struct tspp_pipe_context_regs *tspp_pipe_context;
454 struct tspp_pipe_performance_regs *tspp_pipe_performance;
455
456 struct dentry *dent;
457 struct dentry *debugfs_regs[ARRAY_SIZE(debugfs_tspp_regs)];
458};
459
460
Joel Nider5556a852011-10-16 10:52:13 +0200461static struct class *tspp_class;
462static int tspp_key_entry;
463static dev_t tspp_minor; /* next minor number to assign */
Joel Nider5bd73f82011-12-14 16:53:30 +0200464
465static LIST_HEAD(tspp_devices);
466
467/* forward declarations */
468static ssize_t tspp_read(struct file *, char __user *, size_t, loff_t *);
469static ssize_t tspp_open(struct inode *inode, struct file *filp);
470static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p);
471static ssize_t tspp_release(struct inode *inode, struct file *filp);
472static long tspp_ioctl(struct file *, unsigned int, unsigned long);
473
474/* file operations */
475static const struct file_operations tspp_fops = {
476 .owner = THIS_MODULE,
477 .read = tspp_read,
478 .open = tspp_open,
479 .poll = tspp_poll,
480 .release = tspp_release,
481 .unlocked_ioctl = tspp_ioctl,
482};
Joel Nider5556a852011-10-16 10:52:13 +0200483
484/*** IRQ ***/
Joel Nider5bd73f82011-12-14 16:53:30 +0200485static irqreturn_t tspp_isr(int irq, void *dev)
Joel Nider5556a852011-10-16 10:52:13 +0200486{
Joel Nider5bd73f82011-12-14 16:53:30 +0200487 struct tspp_device *device = dev;
Joel Nider5556a852011-10-16 10:52:13 +0200488 u32 status, mask;
489 u32 data;
490
491 status = readl_relaxed(device->base + TSPP_IRQ_STATUS);
492 mask = readl_relaxed(device->base + TSPP_IRQ_MASK);
493 status &= mask;
494
495 if (!status) {
496 dev_warn(&device->pdev->dev, "Spurious interrupt");
497 return IRQ_NONE;
498 }
499
500 /* if (status & TSPP_IRQ_STATUS_TSP_RD_CMPL) */
501
502 if (status & TSPP_IRQ_STATUS_KEY_ERROR) {
503 /* read the key error info */
504 data = readl_relaxed(device->base + TSPP_KEY_ERROR);
505 dev_info(&device->pdev->dev, "key error 0x%x", data);
506 }
507 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED_BAD) {
508 data = readl_relaxed(device->base + TSPP_KEY_VALID);
509 dev_info(&device->pdev->dev, "key invalidated: 0x%x", data);
510 }
511 if (status & TSPP_IRQ_STATUS_KEY_SWITCHED)
512 dev_info(&device->pdev->dev, "key switched");
513
514 if (status & 0xffff)
Joel Nider5bd73f82011-12-14 16:53:30 +0200515 dev_info(&device->pdev->dev, "broken pipe %i", status & 0xffff);
Joel Nider5556a852011-10-16 10:52:13 +0200516
517 writel_relaxed(status, device->base + TSPP_IRQ_CLEAR);
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200518
519 /*
520 * Before returning IRQ_HANDLED to the generic interrupt handling
521 * framework need to make sure all operations including clearing of
522 * interrupt status registers in the hardware is performed.
523 * Thus a barrier after clearing the interrupt status register
524 * is required to guarantee that the interrupt status register has
525 * really been cleared by the time we return from this handler.
526 */
527 wmb();
528 return IRQ_HANDLED;
529}
530
531static irqreturn_t tsif_isr(int irq, void *dev)
532{
533 struct tspp_tsif_device *tsif_device = dev;
534 u32 sts_ctl = ioread32(tsif_device->base + TSIF_STS_CTL_OFF);
535
536 if (!(sts_ctl & (TSIF_STS_CTL_PACK_AVAIL |
537 TSIF_STS_CTL_OVERFLOW |
538 TSIF_STS_CTL_LOST_SYNC |
539 TSIF_STS_CTL_TIMEOUT)))
540 return IRQ_NONE;
541
542 if (sts_ctl & TSIF_STS_CTL_OVERFLOW)
543 tsif_device->stat_overflow++;
544
545 if (sts_ctl & TSIF_STS_CTL_LOST_SYNC)
546 tsif_device->stat_lost_sync++;
547
548 if (sts_ctl & TSIF_STS_CTL_TIMEOUT)
549 tsif_device->stat_timeout++;
550
551 iowrite32(sts_ctl, tsif_device->base + TSIF_STS_CTL_OFF);
552
553 /*
554 * Before returning IRQ_HANDLED to the generic interrupt handling
555 * framework need to make sure all operations including clearing of
556 * interrupt status registers in the hardware is performed.
557 * Thus a barrier after clearing the interrupt status register
558 * is required to guarantee that the interrupt status register has
559 * really been cleared by the time we return from this handler.
560 */
Joel Nider5556a852011-10-16 10:52:13 +0200561 wmb();
562 return IRQ_HANDLED;
563}
564
565/*** callbacks ***/
566static void tspp_sps_complete_cb(struct sps_event_notify *notify)
567{
Joel Nider5bd73f82011-12-14 16:53:30 +0200568 struct tspp_device *pdev = notify->user;
569 tasklet_schedule(&pdev->tlet);
Joel Nider5556a852011-10-16 10:52:13 +0200570}
571
Hamad Kadmany567bed82012-11-29 14:15:57 +0200572static void tspp_expiration_timer(unsigned long data)
573{
574 struct tspp_device *pdev = (struct tspp_device *)data;
575
576 if (pdev)
577 tasklet_schedule(&pdev->tlet);
578}
579
Joel Nider5556a852011-10-16 10:52:13 +0200580/*** tasklet ***/
581static void tspp_sps_complete_tlet(unsigned long data)
582{
583 int i;
584 int complete;
585 unsigned long flags;
586 struct sps_iovec iovec;
587 struct tspp_channel *channel;
588 struct tspp_device *device = (struct tspp_device *)data;
Joel Nider5556a852011-10-16 10:52:13 +0200589 spin_lock_irqsave(&device->spinlock, flags);
590
591 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
592 complete = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +0200593 channel = &device->channels[i];
Hamad Kadmany567bed82012-11-29 14:15:57 +0200594
Joel Nider5bd73f82011-12-14 16:53:30 +0200595 if (!channel->used || !channel->waiting)
596 continue;
Joel Nider5556a852011-10-16 10:52:13 +0200597
Hamad Kadmany567bed82012-11-29 14:15:57 +0200598 /* stop the expiration timer */
599 if (channel->expiration_period_ms)
600 del_timer(&channel->expiration_timer);
601
Joel Nider5556a852011-10-16 10:52:13 +0200602 /* get completions */
Joel Nider5bd73f82011-12-14 16:53:30 +0200603 while (channel->waiting->state == TSPP_BUF_STATE_WAITING) {
Joel Nider5556a852011-10-16 10:52:13 +0200604 if (sps_get_iovec(channel->pipe, &iovec) != 0) {
605 pr_err("tspp: Error in iovec on channel %i",
606 channel->id);
607 break;
608 }
609 if (iovec.size == 0)
610 break;
611
Joel Nider5bd73f82011-12-14 16:53:30 +0200612 if (iovec.addr != channel->waiting->sps.phys_base)
Joel Nider5556a852011-10-16 10:52:13 +0200613 pr_err("tspp: buffer mismatch 0x%08x",
Joel Nider5bd73f82011-12-14 16:53:30 +0200614 channel->waiting->sps.phys_base);
Joel Nider5556a852011-10-16 10:52:13 +0200615
616 complete = 1;
Joel Nider5bd73f82011-12-14 16:53:30 +0200617 channel->waiting->state = TSPP_BUF_STATE_DATA;
618 channel->waiting->filled = iovec.size;
619 channel->waiting->read_index = 0;
620
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +0200621 if (channel->src == TSPP_SOURCE_TSIF0)
622 device->tsif[0].stat_rx++;
623 else if (channel->src == TSPP_SOURCE_TSIF1)
624 device->tsif[1].stat_rx++;
625
Joel Nider5bd73f82011-12-14 16:53:30 +0200626 /* update the pointers */
627 channel->waiting = channel->waiting->next;
Joel Nider5556a852011-10-16 10:52:13 +0200628 }
629
Joel Nider5bd73f82011-12-14 16:53:30 +0200630 /* wake any waiting processes */
Joel Nider5556a852011-10-16 10:52:13 +0200631 if (complete) {
Joel Nider5556a852011-10-16 10:52:13 +0200632 wake_up_interruptible(&channel->in_queue);
Joel Nider5bd73f82011-12-14 16:53:30 +0200633
634 /* call notifiers */
635 if (channel->notifier)
636 channel->notifier(channel->id,
637 channel->notify_data);
Joel Nider5556a852011-10-16 10:52:13 +0200638 }
Hamad Kadmany567bed82012-11-29 14:15:57 +0200639
640 /* restart expiration timer */
641 if (channel->expiration_period_ms)
642 mod_timer(&channel->expiration_timer,
643 jiffies +
644 MSEC_TO_JIFFIES(
645 channel->expiration_period_ms));
Joel Nider5556a852011-10-16 10:52:13 +0200646 }
647
648 spin_unlock_irqrestore(&device->spinlock, flags);
649}
650
651/*** GPIO functions ***/
Joel Nider5556a852011-10-16 10:52:13 +0200652static int tspp_gpios_disable(const struct msm_gpio *table, int size)
653{
654 int rc = 0;
655 int i;
656 const struct msm_gpio *g;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200657
Joel Nider5556a852011-10-16 10:52:13 +0200658 for (i = size-1; i >= 0; i--) {
659 int tmp;
660 g = table + i;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200661
662 tmp = gpio_tlmm_config(GPIO_CFG(GPIO_PIN(g->gpio_cfg),
663 0, GPIO_CFG_INPUT, GPIO_CFG_PULL_DOWN, GPIO_CFG_2MA),
664 GPIO_CFG_DISABLE);
Joel Nider5556a852011-10-16 10:52:13 +0200665 if (tmp) {
Liron Kuch229090d2012-10-30 17:47:50 +0200666 pr_err("tspp_gpios_disable(0x%08x, GPIO_CFG_DISABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200667 g->gpio_cfg, g->label ?: "?", rc);
668 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
669 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
670 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
671 GPIO_DRVSTR(g->gpio_cfg));
672 if (!rc)
673 rc = tmp;
674 }
675 }
676
677 return rc;
678}
679
680static int tspp_gpios_enable(const struct msm_gpio *table, int size)
681{
682 int rc;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200683 int i, j;
Joel Nider5556a852011-10-16 10:52:13 +0200684 const struct msm_gpio *g;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200685
Joel Nider5556a852011-10-16 10:52:13 +0200686 for (i = 0; i < size; i++) {
687 g = table + i;
688 rc = gpio_tlmm_config(g->gpio_cfg, GPIO_CFG_ENABLE);
689 if (rc) {
Liron Kuch229090d2012-10-30 17:47:50 +0200690 pr_err("tspp: gpio_tlmm_config(0x%08x, GPIO_CFG_ENABLE) <%s> failed: %d\n",
Joel Nider5556a852011-10-16 10:52:13 +0200691 g->gpio_cfg, g->label ?: "?", rc);
692 pr_err("tspp: pin %d func %d dir %d pull %d drvstr %d\n",
693 GPIO_PIN(g->gpio_cfg), GPIO_FUNC(g->gpio_cfg),
694 GPIO_DIR(g->gpio_cfg), GPIO_PULL(g->gpio_cfg),
695 GPIO_DRVSTR(g->gpio_cfg));
696 goto err;
697 }
698 }
699 return 0;
700err:
Liron Kuch8fa85b02013-01-01 18:29:47 +0200701 for (j = 0; j < i; j++)
702 tspp_gpios_disable(table, j);
Joel Nider5556a852011-10-16 10:52:13 +0200703
Joel Nider5556a852011-10-16 10:52:13 +0200704 return rc;
705}
706
Joel Nider5556a852011-10-16 10:52:13 +0200707static int tspp_start_gpios(struct tspp_device *device)
708{
709 struct msm_tspp_platform_data *pdata =
710 device->pdev->dev.platform_data;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200711
712 return tspp_gpios_enable(pdata->gpios, pdata->num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200713}
714
715static void tspp_stop_gpios(struct tspp_device *device)
716{
717 struct msm_tspp_platform_data *pdata =
718 device->pdev->dev.platform_data;
Liron Kuch8fa85b02013-01-01 18:29:47 +0200719
720 tspp_gpios_disable(pdata->gpios, pdata->num_gpios);
Joel Nider5556a852011-10-16 10:52:13 +0200721}
722
Joel Nider5bd73f82011-12-14 16:53:30 +0200723/*** Clock functions ***/
724static int tspp_clock_start(struct tspp_device *device)
725{
726 if (device->tsif_pclk && clk_prepare_enable(device->tsif_pclk) != 0) {
727 pr_err("tspp: Can't start pclk");
728 return -EBUSY;
729 }
730
731 if (device->tsif_ref_clk &&
732 clk_prepare_enable(device->tsif_ref_clk) != 0) {
733 pr_err("tspp: Can't start ref clk");
734 clk_disable_unprepare(device->tsif_pclk);
735 return -EBUSY;
736 }
737
738 return 0;
739}
740
741static void tspp_clock_stop(struct tspp_device *device)
742{
743 if (device->tsif_pclk)
744 clk_disable(device->tsif_pclk);
745
746 if (device->tsif_ref_clk)
747 clk_disable(device->tsif_ref_clk);
748}
749
Joel Nider5556a852011-10-16 10:52:13 +0200750/*** TSIF functions ***/
751static int tspp_start_tsif(struct tspp_tsif_device *tsif_device)
752{
753 int start_hardware = 0;
754 u32 ctl;
755
756 if (tsif_device->ref_count == 0) {
757 start_hardware = 1;
758 } else if (tsif_device->ref_count > 0) {
759 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
760 if ((ctl & TSIF_STS_CTL_START) != 1) {
761 /* this hardware should already be running */
762 pr_warn("tspp: tsif hw not started but ref count > 0");
763 start_hardware = 1;
764 }
765 }
766
767 if (start_hardware) {
Joel Nider5bd73f82011-12-14 16:53:30 +0200768 ctl = TSIF_STS_CTL_EN_IRQ |
Joel Nider5556a852011-10-16 10:52:13 +0200769 TSIF_STS_CTL_EN_DM;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200770
771 if (tsif_device->clock_inverse)
772 ctl |= TSIF_STS_CTL_INV_CLOCK;
773
774 if (tsif_device->data_inverse)
775 ctl |= TSIF_STS_CTL_INV_DATA;
776
777 if (tsif_device->sync_inverse)
778 ctl |= TSIF_STS_CTL_INV_SYNC;
779
780 if (tsif_device->enable_inverse)
781 ctl |= TSIF_STS_CTL_INV_ENABLE;
782
Joel Nider5bd73f82011-12-14 16:53:30 +0200783 switch (tsif_device->mode) {
784 case TSPP_TSIF_MODE_LOOPBACK:
785 ctl |= TSIF_STS_CTL_EN_NULL |
786 TSIF_STS_CTL_EN_ERROR |
787 TSIF_STS_CTL_TEST_MODE;
788 break;
789 case TSPP_TSIF_MODE_1:
790 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
791 TSIF_STS_CTL_EN_TCR;
792 break;
793 case TSPP_TSIF_MODE_2:
794 ctl |= TSIF_STS_CTL_EN_TIME_LIM |
795 TSIF_STS_CTL_EN_TCR |
796 TSIF_STS_CTL_MODE_2;
797 break;
798 default:
799 pr_warn("tspp: unknown tsif mode 0x%x",
800 tsif_device->mode);
Joel Nider5556a852011-10-16 10:52:13 +0200801 }
802 writel_relaxed(ctl, tsif_device->base + TSIF_STS_CTL_OFF);
803 writel_relaxed(tsif_device->time_limit,
804 tsif_device->base + TSIF_TIME_LIMIT_OFF);
805 wmb();
806 writel_relaxed(ctl | TSIF_STS_CTL_START,
807 tsif_device->base + TSIF_STS_CTL_OFF);
808 wmb();
Joel Nider5556a852011-10-16 10:52:13 +0200809 }
810
Joel Nider5bd73f82011-12-14 16:53:30 +0200811 ctl = readl_relaxed(tsif_device->base + TSIF_STS_CTL_OFF);
Joel Nider5556a852011-10-16 10:52:13 +0200812 tsif_device->ref_count++;
813
Joel Nider5bd73f82011-12-14 16:53:30 +0200814 return (ctl & TSIF_STS_CTL_START) ? 0 : -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200815}
816
817static void tspp_stop_tsif(struct tspp_tsif_device *tsif_device)
818{
819 if (tsif_device->ref_count == 0)
820 return;
821
822 tsif_device->ref_count--;
823
824 if (tsif_device->ref_count == 0) {
825 writel_relaxed(TSIF_STS_CTL_STOP,
826 tsif_device->base + TSIF_STS_CTL_OFF);
827 wmb();
828 }
829}
830
Joel Nider5bd73f82011-12-14 16:53:30 +0200831/*** local TSPP functions ***/
832static int tspp_channels_in_use(struct tspp_device *pdev)
833{
834 int i;
835 int count = 0;
836 for (i = 0; i < TSPP_NUM_CHANNELS; i++)
837 count += (pdev->channels[i].used ? 1 : 0);
838
839 return count;
840}
841
842static struct tspp_device *tspp_find_by_id(int id)
843{
844 struct tspp_device *dev;
845 list_for_each_entry(dev, &tspp_devices, devlist) {
846 if (dev->pdev->id == id)
847 return dev;
848 }
849 return NULL;
850}
851
Joel Nider5556a852011-10-16 10:52:13 +0200852static int tspp_get_key_entry(void)
853{
854 int i;
855 for (i = 0; i < TSPP_NUM_KEYS; i++) {
856 if (!(tspp_key_entry & (1 << i))) {
857 tspp_key_entry |= (1 << i);
858 return i;
859 }
860 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200861 return 1 < TSPP_NUM_KEYS;
Joel Nider5556a852011-10-16 10:52:13 +0200862}
863
864static void tspp_free_key_entry(int entry)
865{
866 if (entry > TSPP_NUM_KEYS) {
867 pr_err("tspp_free_key_entry: index out of bounds");
868 return;
869 }
870
871 tspp_key_entry &= ~(1 << entry);
872}
873
Joel Nider5bd73f82011-12-14 16:53:30 +0200874static int tspp_alloc_buffer(u32 channel_id, struct tspp_data_descriptor *desc,
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200875 u32 size, struct dma_pool *dma_pool, tspp_allocator *alloc, void *user)
Joel Nider5556a852011-10-16 10:52:13 +0200876{
Joel Nider5bd73f82011-12-14 16:53:30 +0200877 if (size < TSPP_MIN_BUFFER_SIZE ||
878 size > TSPP_MAX_BUFFER_SIZE) {
879 pr_err("tspp: bad buffer size %i", size);
Joel Nider5556a852011-10-16 10:52:13 +0200880 return -ENOMEM;
881 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200882
883 if (alloc) {
884 TSPP_DEBUG("tspp using alloc function");
885 desc->virt_base = alloc(channel_id, size,
886 &desc->phys_base, user);
887 } else {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200888 if (!dma_pool)
889 desc->virt_base = dma_alloc_coherent(NULL, size,
890 &desc->phys_base, GFP_KERNEL);
891 else
892 desc->virt_base = dma_pool_alloc(dma_pool, GFP_KERNEL,
893 &desc->phys_base);
894
Liron Kuch229090d2012-10-30 17:47:50 +0200895 if (desc->virt_base == 0) {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +0200896 pr_err("tspp: dma buffer allocation failed %i\n", size);
Liron Kuch229090d2012-10-30 17:47:50 +0200897 return -ENOMEM;
898 }
Joel Nider5bd73f82011-12-14 16:53:30 +0200899 }
900
901 desc->size = size;
902 return 0;
903}
904
905static int tspp_queue_buffer(struct tspp_channel *channel,
906 struct tspp_mem_buffer *buffer)
907{
908 int rc;
909 u32 flags = 0;
910
911 /* make sure the interrupt frequency is valid */
912 if (channel->int_freq < 1)
913 channel->int_freq = 1;
914
915 /* generate interrupt according to requested frequency */
916 if (buffer->desc.id % channel->int_freq == channel->int_freq-1)
Hamad Kadmany567bed82012-11-29 14:15:57 +0200917 flags = SPS_IOVEC_FLAG_INT;
Joel Nider5bd73f82011-12-14 16:53:30 +0200918
919 /* start the transfer */
920 rc = sps_transfer_one(channel->pipe,
921 buffer->sps.phys_base,
922 buffer->sps.size,
923 channel->pdev,
924 flags);
925 if (rc < 0)
926 return rc;
927
928 buffer->state = TSPP_BUF_STATE_WAITING;
Joel Nider5556a852011-10-16 10:52:13 +0200929
930 return 0;
931}
932
933static int tspp_global_reset(struct tspp_device *pdev)
934{
935 u32 i, val;
936
937 /* stop all TSIFs */
938 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
939 pdev->tsif[i].ref_count = 1; /* allows stopping hw */
940 tspp_stop_tsif(&pdev->tsif[i]); /* will reset ref_count to 0 */
941 pdev->tsif[i].time_limit = TSPP_TSIF_DEFAULT_TIME_LIMIT;
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +0200942 pdev->tsif[i].clock_inverse = 0;
943 pdev->tsif[i].data_inverse = 0;
944 pdev->tsif[i].sync_inverse = 0;
945 pdev->tsif[i].enable_inverse = 0;
Joel Nider5556a852011-10-16 10:52:13 +0200946 }
947 writel_relaxed(TSPP_RST_RESET, pdev->base + TSPP_RST);
948 wmb();
949
950 /* BAM */
951 if (sps_device_reset(pdev->bam_handle) != 0) {
952 pr_err("tspp: error resetting bam");
Joel Nider5bd73f82011-12-14 16:53:30 +0200953 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +0200954 }
955
956 /* TSPP tables */
957 for (i = 0; i < TSPP_FILTER_TABLES; i++)
Joel Nider5bd73f82011-12-14 16:53:30 +0200958 memset(pdev->filters[i],
Joel Nider5556a852011-10-16 10:52:13 +0200959 0, sizeof(struct tspp_pid_filter_table));
960
961 /* disable all filters */
962 val = (2 << TSPP_NUM_CHANNELS) - 1;
963 writel_relaxed(val, pdev->base + TSPP_PS_DISABLE);
964
965 /* TSPP registers */
966 val = readl_relaxed(pdev->base + TSPP_CONTROL);
967 writel_relaxed(val | TSPP_CLK_CONTROL_FORCE_PERF_CNT,
968 pdev->base + TSPP_CONTROL);
969 wmb();
Joel Nider5bd73f82011-12-14 16:53:30 +0200970 memset(pdev->tspp_global_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +0200971 sizeof(struct tspp_global_performance_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +0200972 memset(pdev->tspp_pipe_context, 0,
Joel Nider5556a852011-10-16 10:52:13 +0200973 sizeof(struct tspp_pipe_context_regs));
Joel Nider5bd73f82011-12-14 16:53:30 +0200974 memset(pdev->tspp_pipe_performance, 0,
Joel Nider5556a852011-10-16 10:52:13 +0200975 sizeof(struct tspp_pipe_performance_regs));
976 wmb();
977 writel_relaxed(val & ~TSPP_CLK_CONTROL_FORCE_PERF_CNT,
978 pdev->base + TSPP_CONTROL);
979 wmb();
980
981 val = readl_relaxed(pdev->base + TSPP_CONFIG);
982 val &= ~(TSPP_CONFIG_PS_LEN_ERR_MASK |
983 TSPP_CONFIG_PS_CONT_ERR_UNSP_MASK |
984 TSPP_CONFIG_PS_CONT_ERR_MASK);
985 TSPP_CONFIG_SET_PACKET_LENGTH(val, TSPP_PACKET_LENGTH);
986 writel_relaxed(val, pdev->base + TSPP_CONFIG);
Hamad Kadmany57f5ac82012-12-20 18:30:40 +0200987 writel_relaxed(0x0007ffff, pdev->base + TSPP_IRQ_MASK);
Joel Nider5556a852011-10-16 10:52:13 +0200988 writel_relaxed(0x000fffff, pdev->base + TSPP_IRQ_CLEAR);
989 writel_relaxed(0, pdev->base + TSPP_RST);
990 wmb();
991
992 tspp_key_entry = 0;
993
994 return 0;
995}
996
Joel Nider5bd73f82011-12-14 16:53:30 +0200997static int tspp_select_source(u32 dev, u32 channel_id,
998 struct tspp_select_source *src)
999{
1000 /* make sure the requested src id is in bounds */
1001 if (src->source > TSPP_SOURCE_MEM) {
1002 pr_err("tspp source out of bounds");
1003 return -EINVAL;
1004 }
1005
1006 /* open the stream */
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001007 tspp_open_stream(dev, channel_id, src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001008
1009 return 0;
1010}
1011
1012static int tspp_set_iv(struct tspp_channel *channel, struct tspp_iv *iv)
1013{
1014 struct tspp_device *pdev = channel->pdev;
1015
1016 writel_relaxed(iv->data[0], pdev->base + TSPP_CBC_INIT_VAL(0));
1017 writel_relaxed(iv->data[1], pdev->base + TSPP_CBC_INIT_VAL(1));
1018 return 0;
1019}
1020
1021static int tspp_set_system_keys(struct tspp_channel *channel,
1022 struct tspp_system_keys *keys)
1023{
1024 int i;
1025 struct tspp_device *pdev = channel->pdev;
1026
1027 for (i = 0; i < TSPP_NUM_SYSTEM_KEYS; i++)
1028 writel_relaxed(keys->data[i], pdev->base + TSPP_SYSTEM_KEY(i));
1029
1030 return 0;
1031}
1032
1033static int tspp_channel_init(struct tspp_channel *channel,
1034 struct tspp_device *pdev)
1035{
1036 channel->cdev.owner = THIS_MODULE;
1037 cdev_init(&channel->cdev, &tspp_fops);
1038 channel->pdev = pdev;
1039 channel->data = NULL;
1040 channel->read = NULL;
1041 channel->waiting = NULL;
1042 channel->locked = NULL;
1043 channel->id = MINOR(tspp_minor);
1044 channel->used = 0;
1045 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1046 channel->max_buffers = TSPP_NUM_BUFFERS;
1047 channel->buffer_count = 0;
1048 channel->filter_count = 0;
1049 channel->int_freq = 1;
Liron Kuch229090d2012-10-30 17:47:50 +02001050 channel->src = TSPP_SOURCE_NONE;
1051 channel->mode = TSPP_MODE_DISABLED;
Joel Nider5bd73f82011-12-14 16:53:30 +02001052 channel->notifier = NULL;
1053 channel->notify_data = NULL;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001054 channel->expiration_period_ms = 0;
Liron Kuch229090d2012-10-30 17:47:50 +02001055 channel->memfree = NULL;
1056 channel->user_info = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001057 init_waitqueue_head(&channel->in_queue);
1058
1059 if (cdev_add(&channel->cdev, tspp_minor++, 1) != 0) {
1060 pr_err("tspp: cdev_add failed");
1061 return -EBUSY;
1062 }
1063
1064 channel->dd = device_create(tspp_class, NULL, channel->cdev.dev,
1065 channel, "tspp%02d", channel->id);
1066 if (IS_ERR(channel->dd)) {
1067 pr_err("tspp: device_create failed: %i",
1068 (int)PTR_ERR(channel->dd));
1069 cdev_del(&channel->cdev);
1070 return -EBUSY;
1071 }
1072
1073 return 0;
1074}
1075
1076static int tspp_set_buffer_size(struct tspp_channel *channel,
1077 struct tspp_buffer *buf)
1078{
Liron Kuch229090d2012-10-30 17:47:50 +02001079 if (channel->buffer_count > 0) {
1080 pr_err("tspp: cannot set buffer size - buffers already allocated\n");
1081 return -EPERM;
1082 }
1083
Joel Nider5bd73f82011-12-14 16:53:30 +02001084 if (buf->size < TSPP_MIN_BUFFER_SIZE)
1085 channel->buffer_size = TSPP_MIN_BUFFER_SIZE;
1086 else if (buf->size > TSPP_MAX_BUFFER_SIZE)
1087 channel->buffer_size = TSPP_MAX_BUFFER_SIZE;
1088 else
1089 channel->buffer_size = buf->size;
1090
1091 return 0;
1092}
1093
1094static void tspp_set_tsif_mode(struct tspp_channel *channel,
1095 enum tspp_tsif_mode mode)
1096{
1097 int index;
1098
1099 switch (channel->src) {
1100 case TSPP_SOURCE_TSIF0:
1101 index = 0;
1102 break;
1103 case TSPP_SOURCE_TSIF1:
1104 index = 1;
1105 break;
1106 default:
1107 pr_warn("tspp: can't set mode for non-tsif source %d",
1108 channel->src);
1109 return;
1110 }
1111 channel->pdev->tsif[index].mode = mode;
1112}
1113
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001114static void tspp_set_signal_inversion(struct tspp_channel *channel,
Liron Kuch229090d2012-10-30 17:47:50 +02001115 int clock_inverse, int data_inverse,
1116 int sync_inverse, int enable_inverse)
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001117{
1118 int index;
1119
1120 switch (channel->src) {
1121 case TSPP_SOURCE_TSIF0:
1122 index = 0;
1123 break;
1124 case TSPP_SOURCE_TSIF1:
1125 index = 1;
1126 break;
1127 default:
1128 return;
1129 }
1130 channel->pdev->tsif[index].clock_inverse = clock_inverse;
1131 channel->pdev->tsif[index].data_inverse = data_inverse;
1132 channel->pdev->tsif[index].sync_inverse = sync_inverse;
1133 channel->pdev->tsif[index].enable_inverse = enable_inverse;
1134}
1135
Liron Kuch229090d2012-10-30 17:47:50 +02001136static int tspp_is_buffer_size_aligned(u32 size, enum tspp_mode mode)
1137{
1138 u32 alignment;
1139
1140 switch (mode) {
1141 case TSPP_MODE_RAW:
1142 /* must be a multiple of 192 */
1143 alignment = (TSPP_PACKET_LENGTH + 4);
1144 if (size % alignment)
1145 return 0;
1146 return 1;
1147
1148 case TSPP_MODE_RAW_NO_SUFFIX:
1149 /* must be a multiple of 188 */
1150 alignment = TSPP_PACKET_LENGTH;
1151 if (size % alignment)
1152 return 0;
1153 return 1;
1154
1155 case TSPP_MODE_DISABLED:
1156 case TSPP_MODE_PES:
1157 default:
1158 /* no alignment requirement */
1159 return 1;
1160 }
1161
1162}
1163
1164static u32 tspp_align_buffer_size_by_mode(u32 size, enum tspp_mode mode)
1165{
1166 u32 new_size;
1167 u32 alignment;
1168
1169 switch (mode) {
1170 case TSPP_MODE_RAW:
1171 /* must be a multiple of 192 */
1172 alignment = (TSPP_PACKET_LENGTH + 4);
1173 break;
1174
1175 case TSPP_MODE_RAW_NO_SUFFIX:
1176 /* must be a multiple of 188 */
1177 alignment = TSPP_PACKET_LENGTH;
1178 break;
1179
1180 case TSPP_MODE_DISABLED:
1181 case TSPP_MODE_PES:
1182 default:
1183 /* no alignment requirement - give the user what he asks for */
1184 alignment = 1;
1185 break;
1186 }
1187 /* align up */
1188 new_size = (((size + alignment - 1) / alignment) * alignment);
1189 return new_size;
1190}
1191
1192static void tspp_destroy_buffers(u32 channel_id, struct tspp_channel *channel)
1193{
1194 int i;
1195 struct tspp_mem_buffer *pbuf, *temp;
1196
1197 pbuf = channel->data;
1198 for (i = 0; i < channel->buffer_count; i++) {
1199 if (pbuf->desc.phys_base) {
1200 if (channel->memfree) {
1201 channel->memfree(channel_id,
1202 pbuf->desc.size,
1203 pbuf->desc.virt_base,
1204 pbuf->desc.phys_base,
1205 channel->user_info);
1206 } else {
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001207 if (!channel->dma_pool)
1208 dma_free_coherent(NULL,
1209 pbuf->desc.size,
1210 pbuf->desc.virt_base,
1211 pbuf->desc.phys_base);
1212 else
1213 dma_pool_free(channel->dma_pool,
1214 pbuf->desc.virt_base,
1215 pbuf->desc.phys_base);
Liron Kuch229090d2012-10-30 17:47:50 +02001216 }
1217 pbuf->desc.phys_base = 0;
1218 }
1219 pbuf->desc.virt_base = 0;
1220 pbuf->state = TSPP_BUF_STATE_EMPTY;
1221 temp = pbuf;
1222 pbuf = pbuf->next;
1223 kfree(temp);
1224 }
1225}
1226
Joel Nider5bd73f82011-12-14 16:53:30 +02001227/*** TSPP API functions ***/
Liron Kuch229090d2012-10-30 17:47:50 +02001228
1229/**
1230 * tspp_open_stream - open a TSPP stream for use.
1231 *
1232 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1233 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1234 * @source: stream source parameters.
1235 *
1236 * Return error status
1237 *
1238 */
1239int tspp_open_stream(u32 dev, u32 channel_id,
1240 struct tspp_select_source *source)
Joel Nider5556a852011-10-16 10:52:13 +02001241{
1242 u32 val;
1243 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001244 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001245
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001246 TSPP_DEBUG("tspp_open_stream %i %i %i %i",
1247 dev, channel_id, source->source, source->mode);
Liron Kuch229090d2012-10-30 17:47:50 +02001248
Joel Nider5bd73f82011-12-14 16:53:30 +02001249 if (dev >= TSPP_MAX_DEVICES) {
1250 pr_err("tspp: device id out of range");
1251 return -ENODEV;
1252 }
Joel Nider5556a852011-10-16 10:52:13 +02001253
Joel Nider5bd73f82011-12-14 16:53:30 +02001254 if (channel_id >= TSPP_NUM_CHANNELS) {
1255 pr_err("tspp: channel id out of range");
1256 return -ECHRNG;
1257 }
1258
1259 pdev = tspp_find_by_id(dev);
1260 if (!pdev) {
1261 pr_err("tspp_str: can't find device %i", dev);
1262 return -ENODEV;
1263 }
1264 channel = &pdev->channels[channel_id];
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001265 channel->src = source->source;
1266 tspp_set_tsif_mode(channel, source->mode);
1267 tspp_set_signal_inversion(channel, source->clk_inverse,
Liron Kuch229090d2012-10-30 17:47:50 +02001268 source->data_inverse, source->sync_inverse,
1269 source->enable_inverse);
Joel Nider5556a852011-10-16 10:52:13 +02001270
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001271 switch (source->source) {
Joel Nider5556a852011-10-16 10:52:13 +02001272 case TSPP_SOURCE_TSIF0:
1273 /* make sure TSIF0 is running & enabled */
1274 if (tspp_start_tsif(&pdev->tsif[0]) != 0) {
1275 pr_err("tspp: error starting tsif0");
Joel Nider5bd73f82011-12-14 16:53:30 +02001276 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001277 }
1278 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1279 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1280 pdev->base + TSPP_CONTROL);
1281 wmb();
1282 break;
1283 case TSPP_SOURCE_TSIF1:
1284 /* make sure TSIF1 is running & enabled */
1285 if (tspp_start_tsif(&pdev->tsif[1]) != 0) {
1286 pr_err("tspp: error starting tsif1");
Joel Nider5bd73f82011-12-14 16:53:30 +02001287 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001288 }
1289 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1290 writel_relaxed(val & ~TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1291 pdev->base + TSPP_CONTROL);
1292 wmb();
1293 break;
1294 case TSPP_SOURCE_MEM:
1295 break;
1296 default:
Hamad Kadmanybbd06bf2012-10-23 14:15:41 +02001297 pr_err("tspp: channel %i invalid source %i",
1298 channel->id, source->source);
Joel Nider5bd73f82011-12-14 16:53:30 +02001299 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001300 }
1301
Joel Nider5556a852011-10-16 10:52:13 +02001302 return 0;
1303}
1304EXPORT_SYMBOL(tspp_open_stream);
1305
Liron Kuch229090d2012-10-30 17:47:50 +02001306/**
1307 * tspp_close_stream - close a TSPP stream.
1308 *
1309 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1310 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1311 *
1312 * Return error status
1313 *
1314 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001315int tspp_close_stream(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001316{
1317 u32 val;
1318 struct tspp_device *pdev;
Joel Nider5bd73f82011-12-14 16:53:30 +02001319 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001320
Joel Nider5bd73f82011-12-14 16:53:30 +02001321 if (channel_id >= TSPP_NUM_CHANNELS) {
1322 pr_err("tspp: channel id out of range");
1323 return -ECHRNG;
1324 }
1325 pdev = tspp_find_by_id(dev);
1326 if (!pdev) {
1327 pr_err("tspp_cs: can't find device %i", dev);
1328 return -EBUSY;
1329 }
1330 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001331
1332 switch (channel->src) {
1333 case TSPP_SOURCE_TSIF0:
1334 tspp_stop_tsif(&pdev->tsif[0]);
1335 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1336 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF0_SRC_DIS,
1337 pdev->base + TSPP_CONTROL);
1338 wmb();
1339 break;
1340 case TSPP_SOURCE_TSIF1:
1341 tspp_stop_tsif(&pdev->tsif[1]);
1342 val = readl_relaxed(pdev->base + TSPP_CONTROL);
1343 writel_relaxed(val | TSPP_CONTROL_TSP_TSIF1_SRC_DIS,
1344 pdev->base + TSPP_CONTROL);
1345 break;
1346 case TSPP_SOURCE_MEM:
1347 break;
1348 case TSPP_SOURCE_NONE:
1349 break;
1350 }
1351
Joel Nider5bd73f82011-12-14 16:53:30 +02001352 channel->src = TSPP_SOURCE_NONE;
Joel Nider5556a852011-10-16 10:52:13 +02001353 return 0;
1354}
1355EXPORT_SYMBOL(tspp_close_stream);
1356
Liron Kuch229090d2012-10-30 17:47:50 +02001357/**
1358 * tspp_open_channel - open a TSPP channel.
1359 *
1360 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1361 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1362 *
1363 * Return error status
1364 *
1365 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001366int tspp_open_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001367{
1368 int rc = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001369 struct sps_connect *config;
1370 struct sps_register_event *event;
1371 struct tspp_channel *channel;
1372 struct tspp_device *pdev;
1373
1374 if (channel_id >= TSPP_NUM_CHANNELS) {
1375 pr_err("tspp: channel id out of range");
1376 return -ECHRNG;
1377 }
1378 pdev = tspp_find_by_id(dev);
1379 if (!pdev) {
1380 pr_err("tspp_oc: can't find device %i", dev);
1381 return -ENODEV;
1382 }
1383 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001384
1385 if (channel->used) {
1386 pr_err("tspp channel already in use");
Joel Nider5bd73f82011-12-14 16:53:30 +02001387 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02001388 }
1389
Joel Nider5bd73f82011-12-14 16:53:30 +02001390 config = &channel->config;
1391 event = &channel->event;
1392
1393 /* start the clocks if needed */
Liron Kuch8fa85b02013-01-01 18:29:47 +02001394 if (tspp_channels_in_use(pdev) == 0) {
1395 tspp_clock_start(pdev);
Joel Nider5bd73f82011-12-14 16:53:30 +02001396 wake_lock(&pdev->wake_lock);
Liron Kuch8fa85b02013-01-01 18:29:47 +02001397 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001398
Joel Nider5556a852011-10-16 10:52:13 +02001399 /* mark it as used */
1400 channel->used = 1;
1401
1402 /* start the bam */
1403 channel->pipe = sps_alloc_endpoint();
1404 if (channel->pipe == 0) {
1405 pr_err("tspp: error allocating endpoint");
1406 rc = -ENOMEM;
1407 goto err_sps_alloc;
1408 }
1409
1410 /* get default configuration */
1411 sps_get_config(channel->pipe, config);
1412
Joel Nider5bd73f82011-12-14 16:53:30 +02001413 config->source = pdev->bam_handle;
Joel Nider5556a852011-10-16 10:52:13 +02001414 config->destination = SPS_DEV_HANDLE_MEM;
1415 config->mode = SPS_MODE_SRC;
Joel Nider5bd73f82011-12-14 16:53:30 +02001416 config->options =
1417 SPS_O_AUTO_ENABLE | /* connection is auto-enabled */
1418 SPS_O_STREAMING | /* streaming mode */
1419 SPS_O_DESC_DONE | /* interrupt on end of descriptor */
Hamad Kadmany567bed82012-11-29 14:15:57 +02001420 SPS_O_ACK_TRANSFERS | /* must use sps_get_iovec() */
1421 SPS_O_HYBRID; /* Read actual descriptors in sps_get_iovec() */
Joel Nider5556a852011-10-16 10:52:13 +02001422 config->src_pipe_index = channel->id;
1423 config->desc.size =
Hamad Kadmany567bed82012-11-29 14:15:57 +02001424 TSPP_SPS_DESCRIPTOR_COUNT * SPS_DESCRIPTOR_SIZE;
Joel Nider5556a852011-10-16 10:52:13 +02001425 config->desc.base = dma_alloc_coherent(NULL,
1426 config->desc.size,
1427 &config->desc.phys_base,
1428 GFP_KERNEL);
1429 if (config->desc.base == 0) {
1430 pr_err("tspp: error allocating sps descriptors");
1431 rc = -ENOMEM;
1432 goto err_desc_alloc;
1433 }
1434
1435 memset(config->desc.base, 0, config->desc.size);
1436
1437 rc = sps_connect(channel->pipe, config);
1438 if (rc) {
1439 pr_err("tspp: error connecting bam");
1440 goto err_connect;
1441 }
1442
1443 event->mode = SPS_TRIGGER_CALLBACK;
Joel Nider5bd73f82011-12-14 16:53:30 +02001444 event->options = SPS_O_DESC_DONE;
Joel Nider5556a852011-10-16 10:52:13 +02001445 event->callback = tspp_sps_complete_cb;
1446 event->xfer_done = NULL;
Joel Nider5bd73f82011-12-14 16:53:30 +02001447 event->user = pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001448
1449 rc = sps_register_event(channel->pipe, event);
1450 if (rc) {
1451 pr_err("tspp: error registering event");
1452 goto err_event;
1453 }
1454
Hamad Kadmany567bed82012-11-29 14:15:57 +02001455 init_timer(&channel->expiration_timer);
1456 channel->expiration_timer.function = tspp_expiration_timer;
1457 channel->expiration_timer.data = (unsigned long)pdev;
1458 channel->expiration_timer.expires = 0xffffffffL;
1459
Joel Nider5bd73f82011-12-14 16:53:30 +02001460 rc = pm_runtime_get(&pdev->pdev->dev);
Joel Nider5556a852011-10-16 10:52:13 +02001461 if (rc < 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001462 dev_err(&pdev->pdev->dev,
Joel Nider5556a852011-10-16 10:52:13 +02001463 "Runtime PM: Unable to wake up tspp device, rc = %d",
1464 rc);
1465 }
Joel Nider5556a852011-10-16 10:52:13 +02001466 return 0;
1467
1468err_event:
1469 sps_disconnect(channel->pipe);
1470err_connect:
1471 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1472 config->desc.phys_base);
1473err_desc_alloc:
1474 sps_free_endpoint(channel->pipe);
1475err_sps_alloc:
1476 return rc;
1477}
1478EXPORT_SYMBOL(tspp_open_channel);
1479
Liron Kuch229090d2012-10-30 17:47:50 +02001480/**
1481 * tspp_close_channel - close a TSPP channel.
1482 *
1483 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1484 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1485 *
1486 * Return error status
1487 *
1488 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001489int tspp_close_channel(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001490{
1491 int i;
1492 int id;
1493 u32 val;
Joel Nider5556a852011-10-16 10:52:13 +02001494
Joel Nider5bd73f82011-12-14 16:53:30 +02001495 struct sps_connect *config;
1496 struct tspp_device *pdev;
1497 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02001498
1499 if (channel_id >= TSPP_NUM_CHANNELS) {
1500 pr_err("tspp: channel id out of range");
1501 return -ECHRNG;
1502 }
1503 pdev = tspp_find_by_id(dev);
1504 if (!pdev) {
1505 pr_err("tspp_close: can't find device %i", dev);
1506 return -ENODEV;
1507 }
1508 channel = &pdev->channels[channel_id];
1509
1510 /* if the channel is not used, we are done */
1511 if (!channel->used)
1512 return 0;
1513
Hamad Kadmany567bed82012-11-29 14:15:57 +02001514 if (channel->expiration_period_ms)
1515 del_timer(&channel->expiration_timer);
1516
Joel Nider5bd73f82011-12-14 16:53:30 +02001517 channel->notifier = NULL;
1518 channel->notify_data = NULL;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001519 channel->expiration_period_ms = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001520
1521 config = &channel->config;
1522 pdev = channel->pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001523
1524 /* disable pipe (channel) */
1525 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1526 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1527 wmb();
1528
1529 /* unregister all filters for this channel */
1530 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1531 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001532 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001533 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1534 if (id == channel->id) {
1535 if (FILTER_HAS_ENCRYPTION(tspp_filter))
1536 tspp_free_key_entry(
1537 FILTER_GET_KEY_NUMBER(tspp_filter));
1538 tspp_filter->config = 0;
1539 tspp_filter->filter = 0;
1540 }
1541 }
1542 channel->filter_count = 0;
1543
1544 /* stop the stream */
Joel Nider5bd73f82011-12-14 16:53:30 +02001545 tspp_close_stream(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02001546
1547 /* disconnect the bam */
1548 if (sps_disconnect(channel->pipe) != 0)
1549 pr_warn("tspp: Error freeing sps endpoint (%i)", channel->id);
1550
1551 /* destroy the buffers */
1552 dma_free_coherent(NULL, config->desc.size, config->desc.base,
1553 config->desc.phys_base);
1554
Liron Kuch229090d2012-10-30 17:47:50 +02001555 tspp_destroy_buffers(channel_id, channel);
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001556 if (channel->dma_pool) {
1557 dma_pool_destroy(channel->dma_pool);
1558 channel->dma_pool = NULL;
1559 }
Liron Kuch229090d2012-10-30 17:47:50 +02001560
1561 channel->src = TSPP_SOURCE_NONE;
1562 channel->mode = TSPP_MODE_DISABLED;
1563 channel->memfree = NULL;
1564 channel->user_info = NULL;
Joel Nider5556a852011-10-16 10:52:13 +02001565 channel->buffer_count = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001566 channel->data = NULL;
1567 channel->read = NULL;
1568 channel->waiting = NULL;
1569 channel->locked = NULL;
1570 channel->used = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001571
Liron Kuch8fa85b02013-01-01 18:29:47 +02001572 if (tspp_channels_in_use(pdev) == 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001573 wake_unlock(&pdev->wake_lock);
Liron Kuch8fa85b02013-01-01 18:29:47 +02001574 tspp_clock_stop(pdev);
1575 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001576
Joel Nider5556a852011-10-16 10:52:13 +02001577 return 0;
1578}
1579EXPORT_SYMBOL(tspp_close_channel);
1580
Liron Kuch229090d2012-10-30 17:47:50 +02001581/**
Hamad Kadmany586fb392013-01-31 14:49:20 +02001582 * tspp_get_ref_clk_counter - return the TSIF clock reference (TCR) counter.
1583 *
1584 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1585 * @source: The TSIF source from which the counter should be read
1586 * @tcr_counter: the value of TCR counter
1587 *
1588 * Return error status
1589 *
1590 * TCR increments at a rate equal to 27 MHz/256 = 105.47 kHz.
1591 * If source is neither TSIF 0 or TSIF1 0 is returned.
1592 */
1593int tspp_get_ref_clk_counter(u32 dev, enum tspp_source source, u32 *tcr_counter)
1594{
1595 struct tspp_device *pdev;
1596 struct tspp_tsif_device *tsif_device;
1597
1598 if (!tcr_counter)
1599 return -EINVAL;
1600
1601 pdev = tspp_find_by_id(dev);
1602 if (!pdev) {
1603 pr_err("tspp_get_ref_clk_counter: can't find device %i\n", dev);
1604 return -ENODEV;
1605 }
1606
1607 switch (source) {
1608 case TSPP_SOURCE_TSIF0:
1609 tsif_device = &pdev->tsif[0];
1610 break;
1611
1612 case TSPP_SOURCE_TSIF1:
1613 tsif_device = &pdev->tsif[1];
1614 break;
1615
1616 default:
1617 tsif_device = NULL;
1618 break;
1619 }
1620
1621 if (tsif_device && tsif_device->ref_count)
1622 *tcr_counter = ioread32(tsif_device->base + TSIF_CLK_REF_OFF);
1623 else
1624 *tcr_counter = 0;
1625
1626 return 0;
1627}
1628EXPORT_SYMBOL(tspp_get_ref_clk_counter);
1629
1630/**
Liron Kuch229090d2012-10-30 17:47:50 +02001631 * tspp_add_filter - add a TSPP filter to a channel.
1632 *
1633 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1634 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1635 * @filter: TSPP filter parameters
1636 *
1637 * Return error status
1638 *
1639 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001640int tspp_add_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001641 struct tspp_filter *filter)
1642{
Liron Kuch229090d2012-10-30 17:47:50 +02001643 int i, rc;
Joel Nider5556a852011-10-16 10:52:13 +02001644 int other_channel;
1645 int entry;
1646 u32 val, pid, enabled;
Joel Nider5bd73f82011-12-14 16:53:30 +02001647 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001648 struct tspp_pid_filter p;
Joel Nider5bd73f82011-12-14 16:53:30 +02001649 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02001650
Joel Nider5bd73f82011-12-14 16:53:30 +02001651 TSPP_DEBUG("tspp: add filter");
1652 if (channel_id >= TSPP_NUM_CHANNELS) {
1653 pr_err("tspp: channel id out of range");
1654 return -ECHRNG;
1655 }
1656 pdev = tspp_find_by_id(dev);
1657 if (!pdev) {
1658 pr_err("tspp_add: can't find device %i", dev);
1659 return -ENODEV;
1660 }
1661
1662 channel = &pdev->channels[channel_id];
1663
Joel Nider5556a852011-10-16 10:52:13 +02001664 if (filter->source > TSPP_SOURCE_MEM) {
1665 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001666 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001667 }
1668
1669 if (filter->priority >= TSPP_NUM_PRIORITIES) {
1670 pr_err("tspp invalid source");
Joel Nider5bd73f82011-12-14 16:53:30 +02001671 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001672 }
1673
Liron Kuch229090d2012-10-30 17:47:50 +02001674 channel->mode = filter->mode;
1675 /*
1676 * if buffers are already allocated, verify they fulfil
1677 * the alignment requirements.
1678 */
1679 if ((channel->buffer_count > 0) &&
1680 (!tspp_is_buffer_size_aligned(channel->buffer_size, channel->mode)))
1681 pr_warn("tspp: buffers allocated with incorrect alignment\n");
Joel Nider5556a852011-10-16 10:52:13 +02001682
1683 if (filter->mode == TSPP_MODE_PES) {
1684 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1685 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001686 &pdev->filters[channel->src]->filter[i];
Joel Nider5556a852011-10-16 10:52:13 +02001687 pid = FILTER_GET_PIPE_PID((tspp_filter));
1688 enabled = FILTER_GET_PIPE_PROCESS0(tspp_filter);
1689 if (enabled && (pid == filter->pid)) {
1690 other_channel =
1691 FILTER_GET_PIPE_NUMBER0(tspp_filter);
1692 pr_err("tspp: pid 0x%x already in use by channel %i",
1693 filter->pid, other_channel);
Joel Nider5bd73f82011-12-14 16:53:30 +02001694 return -EBADSLT;
Joel Nider5556a852011-10-16 10:52:13 +02001695 }
1696 }
1697 }
1698
1699 /* make sure this priority is not already in use */
1700 enabled = FILTER_GET_PIPE_PROCESS0(
Joel Nider5bd73f82011-12-14 16:53:30 +02001701 (&(pdev->filters[channel->src]->filter[filter->priority])));
Joel Nider5556a852011-10-16 10:52:13 +02001702 if (enabled) {
1703 pr_err("tspp: filter priority %i source %i is already enabled\n",
1704 filter->priority, channel->src);
Joel Nider5bd73f82011-12-14 16:53:30 +02001705 return -ENOSR;
Joel Nider5556a852011-10-16 10:52:13 +02001706 }
1707
1708 if (channel->mode == TSPP_MODE_PES) {
1709 /* if we are already processing in PES mode, disable pipe
1710 (channel) and filter to be updated */
1711 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1712 writel_relaxed(val | (1 << channel->id),
1713 pdev->base + TSPP_PS_DISABLE);
1714 wmb();
1715 }
1716
1717 /* update entry */
1718 p.filter = 0;
Joel Nider5bd73f82011-12-14 16:53:30 +02001719 p.config = FILTER_TRANS_END_DISABLE;
Joel Nider5556a852011-10-16 10:52:13 +02001720 FILTER_SET_PIPE_PROCESS0((&p), filter->mode);
1721 FILTER_SET_PIPE_PID((&p), filter->pid);
1722 FILTER_SET_PID_MASK((&p), filter->mask);
1723 FILTER_SET_PIPE_NUMBER0((&p), channel->id);
1724 FILTER_SET_PIPE_PROCESS1((&p), TSPP_MODE_DISABLED);
1725 if (filter->decrypt) {
1726 entry = tspp_get_key_entry();
1727 if (entry == -1) {
1728 pr_err("tspp: no more keys available!");
1729 } else {
1730 p.config |= FILTER_DECRYPT;
1731 FILTER_SET_KEY_NUMBER((&p), entry);
1732 }
1733 }
Joel Nider5556a852011-10-16 10:52:13 +02001734
Joel Nider5bd73f82011-12-14 16:53:30 +02001735 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001736 filter[filter->priority].config = p.config;
Joel Nider5bd73f82011-12-14 16:53:30 +02001737 pdev->filters[channel->src]->
Joel Nider5556a852011-10-16 10:52:13 +02001738 filter[filter->priority].filter = p.filter;
1739
Liron Kuch229090d2012-10-30 17:47:50 +02001740 /*
1741 * allocate buffers if needed (i.e. if user did has not already called
1742 * tspp_allocate_buffers() explicitly).
1743 */
1744 if (channel->buffer_count == 0) {
1745 channel->buffer_size =
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02001746 tspp_align_buffer_size_by_mode(channel->buffer_size,
Liron Kuch229090d2012-10-30 17:47:50 +02001747 channel->mode);
1748 rc = tspp_allocate_buffers(dev, channel->id,
1749 channel->max_buffers,
1750 channel->buffer_size,
1751 channel->int_freq, NULL, NULL, NULL);
1752 if (rc != 0) {
1753 pr_err("tspp: tspp_allocate_buffers failed\n");
1754 return rc;
1755 }
Joel Nider5bd73f82011-12-14 16:53:30 +02001756 }
1757
Joel Nider5556a852011-10-16 10:52:13 +02001758 /* reenable pipe */
1759 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1760 writel_relaxed(val & ~(1 << channel->id), pdev->base + TSPP_PS_DISABLE);
1761 wmb();
1762 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1763
Joel Nider5556a852011-10-16 10:52:13 +02001764 channel->filter_count++;
1765
1766 return 0;
1767}
1768EXPORT_SYMBOL(tspp_add_filter);
1769
Liron Kuch229090d2012-10-30 17:47:50 +02001770/**
1771 * tspp_remove_filter - remove a TSPP filter from a channel.
1772 *
1773 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1774 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1775 * @filter: TSPP filter parameters
1776 *
1777 * Return error status
1778 *
1779 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001780int tspp_remove_filter(u32 dev, u32 channel_id,
Joel Nider5556a852011-10-16 10:52:13 +02001781 struct tspp_filter *filter)
1782{
1783 int entry;
1784 u32 val;
Joel Nider5bd73f82011-12-14 16:53:30 +02001785 struct tspp_device *pdev;
1786 int src;
1787 struct tspp_pid_filter *tspp_filter;
1788 struct tspp_channel *channel;
1789
1790 if (channel_id >= TSPP_NUM_CHANNELS) {
1791 pr_err("tspp: channel id out of range");
1792 return -ECHRNG;
1793 }
1794 pdev = tspp_find_by_id(dev);
1795 if (!pdev) {
1796 pr_err("tspp_remove: can't find device %i", dev);
1797 return -ENODEV;
1798 }
1799 channel = &pdev->channels[channel_id];
1800
1801 src = channel->src;
1802 tspp_filter = &(pdev->filters[src]->filter[filter->priority]);
Joel Nider5556a852011-10-16 10:52:13 +02001803
1804 /* disable pipe (channel) */
1805 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1806 writel_relaxed(val | channel->id, pdev->base + TSPP_PS_DISABLE);
1807 wmb();
1808
1809 /* update data keys */
1810 if (tspp_filter->config & FILTER_DECRYPT) {
1811 entry = FILTER_GET_KEY_NUMBER(tspp_filter);
1812 tspp_free_key_entry(entry);
1813 }
1814
1815 /* update pid table */
1816 tspp_filter->config = 0;
1817 tspp_filter->filter = 0;
1818
1819 channel->filter_count--;
1820
1821 /* reenable pipe */
1822 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1823 writel_relaxed(val & ~(1 << channel->id),
1824 pdev->base + TSPP_PS_DISABLE);
1825 wmb();
1826 val = readl_relaxed(pdev->base + TSPP_PS_DISABLE);
1827
1828 return 0;
1829}
1830EXPORT_SYMBOL(tspp_remove_filter);
1831
Liron Kuch229090d2012-10-30 17:47:50 +02001832/**
1833 * tspp_set_key - set TSPP key in key table.
1834 *
1835 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1836 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1837 * @key: TSPP key parameters
1838 *
1839 * Return error status
1840 *
1841 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001842int tspp_set_key(u32 dev, u32 channel_id, struct tspp_key *key)
Joel Nider5556a852011-10-16 10:52:13 +02001843{
1844 int i;
1845 int id;
1846 int key_index;
1847 int data;
Joel Nider5bd73f82011-12-14 16:53:30 +02001848 struct tspp_channel *channel;
1849 struct tspp_device *pdev;
1850
1851 if (channel_id >= TSPP_NUM_CHANNELS) {
1852 pr_err("tspp: channel id out of range");
1853 return -ECHRNG;
1854 }
1855 pdev = tspp_find_by_id(dev);
1856 if (!pdev) {
1857 pr_err("tspp_set: can't find device %i", dev);
1858 return -ENODEV;
1859 }
1860 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001861
1862 /* read the key index used by this channel */
1863 for (i = 0; i < TSPP_NUM_PRIORITIES; i++) {
1864 struct tspp_pid_filter *tspp_filter =
Joel Nider5bd73f82011-12-14 16:53:30 +02001865 &(pdev->filters[channel->src]->filter[i]);
Joel Nider5556a852011-10-16 10:52:13 +02001866 id = FILTER_GET_PIPE_NUMBER0(tspp_filter);
1867 if (id == channel->id) {
1868 if (FILTER_HAS_ENCRYPTION(tspp_filter)) {
1869 key_index = FILTER_GET_KEY_NUMBER(tspp_filter);
1870 break;
1871 }
1872 }
1873 }
1874 if (i == TSPP_NUM_PRIORITIES) {
1875 pr_err("tspp: no encryption on this channel");
Joel Nider5bd73f82011-12-14 16:53:30 +02001876 return -ENOKEY;
Joel Nider5556a852011-10-16 10:52:13 +02001877 }
1878
1879 if (key->parity == TSPP_KEY_PARITY_EVEN) {
Joel Nider5bd73f82011-12-14 16:53:30 +02001880 pdev->tspp_key_table->entry[key_index].even_lsb = key->lsb;
1881 pdev->tspp_key_table->entry[key_index].even_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001882 } else {
Joel Nider5bd73f82011-12-14 16:53:30 +02001883 pdev->tspp_key_table->entry[key_index].odd_lsb = key->lsb;
1884 pdev->tspp_key_table->entry[key_index].odd_msb = key->msb;
Joel Nider5556a852011-10-16 10:52:13 +02001885 }
1886 data = readl_relaxed(channel->pdev->base + TSPP_KEY_VALID);
1887
1888 return 0;
1889}
1890EXPORT_SYMBOL(tspp_set_key);
1891
Liron Kuch229090d2012-10-30 17:47:50 +02001892/**
1893 * tspp_register_notification - register TSPP channel notification function.
1894 *
1895 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1896 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1897 * @pNotify: notification function
1898 * @userdata: user data to pass to notification function
1899 * @timer_ms: notification for partially filled buffers
1900 *
1901 * Return error status
1902 *
1903 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001904int tspp_register_notification(u32 dev, u32 channel_id,
1905 tspp_notifier *pNotify, void *userdata, u32 timer_ms)
Joel Nider5556a852011-10-16 10:52:13 +02001906{
Joel Nider5bd73f82011-12-14 16:53:30 +02001907 struct tspp_channel *channel;
1908 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001909
Joel Nider5bd73f82011-12-14 16:53:30 +02001910 if (channel_id >= TSPP_NUM_CHANNELS) {
1911 pr_err("tspp: channel id out of range");
1912 return -ECHRNG;
1913 }
1914 pdev = tspp_find_by_id(dev);
1915 if (!pdev) {
1916 pr_err("tspp_reg: can't find device %i", dev);
1917 return -ENODEV;
1918 }
1919 channel = &pdev->channels[channel_id];
1920 channel->notifier = pNotify;
1921 channel->notify_data = userdata;
Hamad Kadmany567bed82012-11-29 14:15:57 +02001922 channel->expiration_period_ms = timer_ms;
1923
Joel Nider5556a852011-10-16 10:52:13 +02001924 return 0;
1925}
Joel Nider5bd73f82011-12-14 16:53:30 +02001926EXPORT_SYMBOL(tspp_register_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001927
Liron Kuch229090d2012-10-30 17:47:50 +02001928/**
1929 * tspp_unregister_notification - unregister TSPP channel notification function.
1930 *
1931 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1932 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1933 *
1934 * Return error status
1935 *
1936 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001937int tspp_unregister_notification(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001938{
Joel Nider5bd73f82011-12-14 16:53:30 +02001939 struct tspp_channel *channel;
1940 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001941
Joel Nider5bd73f82011-12-14 16:53:30 +02001942 if (channel_id >= TSPP_NUM_CHANNELS) {
1943 pr_err("tspp: channel id out of range");
1944 return -ECHRNG;
1945 }
1946 pdev = tspp_find_by_id(dev);
1947 if (!pdev) {
1948 pr_err("tspp_unreg: can't find device %i", dev);
1949 return -ENODEV;
1950 }
1951 channel = &pdev->channels[channel_id];
1952 channel->notifier = NULL;
1953 channel->notify_data = 0;
Joel Nider5556a852011-10-16 10:52:13 +02001954 return 0;
1955}
Joel Nider5bd73f82011-12-14 16:53:30 +02001956EXPORT_SYMBOL(tspp_unregister_notification);
Joel Nider5556a852011-10-16 10:52:13 +02001957
Liron Kuch229090d2012-10-30 17:47:50 +02001958/**
1959 * tspp_get_buffer - get TSPP data buffer.
1960 *
1961 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
1962 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
1963 *
1964 * Return error status
1965 *
1966 */
Joel Nider5bd73f82011-12-14 16:53:30 +02001967const struct tspp_data_descriptor *tspp_get_buffer(u32 dev, u32 channel_id)
Joel Nider5556a852011-10-16 10:52:13 +02001968{
Joel Nider5bd73f82011-12-14 16:53:30 +02001969 struct tspp_mem_buffer *buffer;
1970 struct tspp_channel *channel;
1971 struct tspp_device *pdev;
Joel Nider5556a852011-10-16 10:52:13 +02001972
Joel Nider5bd73f82011-12-14 16:53:30 +02001973 if (channel_id >= TSPP_NUM_CHANNELS) {
1974 pr_err("tspp: channel id out of range");
1975 return NULL;
1976 }
1977 pdev = tspp_find_by_id(dev);
1978 if (!pdev) {
1979 pr_err("tspp_get: can't find device %i", dev);
1980 return NULL;
1981 }
1982 channel = &pdev->channels[channel_id];
Joel Nider5556a852011-10-16 10:52:13 +02001983
Joel Nider5bd73f82011-12-14 16:53:30 +02001984 if (!channel->read) {
1985 pr_warn("tspp: no buffer to get on channel %i!",
1986 channel->id);
1987 return NULL;
1988 }
1989
1990 buffer = channel->read;
1991 /* see if we have any buffers ready to read */
1992 if (buffer->state != TSPP_BUF_STATE_DATA)
1993 return 0;
1994
1995 if (buffer->state == TSPP_BUF_STATE_DATA) {
1996 /* mark the buffer as busy */
1997 buffer->state = TSPP_BUF_STATE_LOCKED;
1998
1999 /* increment the pointer along the list */
2000 channel->read = channel->read->next;
2001 }
2002
2003 return &buffer->desc;
2004}
2005EXPORT_SYMBOL(tspp_get_buffer);
2006
Liron Kuch229090d2012-10-30 17:47:50 +02002007/**
2008 * tspp_release_buffer - release TSPP data buffer back to TSPP.
2009 *
2010 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2011 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2012 * @descriptor_id: buffer descriptor ID
2013 *
2014 * Return error status
2015 *
2016 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002017int tspp_release_buffer(u32 dev, u32 channel_id, u32 descriptor_id)
2018{
2019 int i, found = 0;
2020 struct tspp_mem_buffer *buffer;
2021 struct tspp_channel *channel;
2022 struct tspp_device *pdev;
2023
2024 if (channel_id >= TSPP_NUM_CHANNELS) {
2025 pr_err("tspp: channel id out of range");
2026 return -ECHRNG;
2027 }
2028 pdev = tspp_find_by_id(dev);
2029 if (!pdev) {
2030 pr_err("tspp: can't find device %i", dev);
2031 return -ENODEV;
2032 }
2033 channel = &pdev->channels[channel_id];
2034
2035 if (descriptor_id > channel->buffer_count)
2036 pr_warn("tspp: desc id looks weird 0x%08x", descriptor_id);
2037
2038 /* find the correct descriptor */
2039 buffer = channel->locked;
2040 for (i = 0; i < channel->buffer_count; i++) {
2041 if (buffer->desc.id == descriptor_id) {
2042 found = 1;
2043 break;
2044 }
2045 buffer = buffer->next;
2046 }
2047 channel->locked = channel->locked->next;
2048
2049 if (!found) {
2050 pr_err("tspp: cant find desc %i", descriptor_id);
2051 return -EINVAL;
2052 }
2053
2054 /* make sure the buffer is in the expected state */
2055 if (buffer->state != TSPP_BUF_STATE_LOCKED) {
2056 pr_err("tspp: buffer %i not locked", descriptor_id);
2057 return -EINVAL;
2058 }
2059 /* unlock the buffer and requeue it */
2060 buffer->state = TSPP_BUF_STATE_WAITING;
2061
2062 if (tspp_queue_buffer(channel, buffer))
2063 pr_warn("tspp: can't requeue buffer");
Joel Nider5556a852011-10-16 10:52:13 +02002064 return 0;
2065}
Joel Nider5bd73f82011-12-14 16:53:30 +02002066EXPORT_SYMBOL(tspp_release_buffer);
2067
Liron Kuch229090d2012-10-30 17:47:50 +02002068/**
2069 * tspp_allocate_buffers - allocate TSPP data buffers.
2070 *
2071 * @dev: TSPP device (up to TSPP_MAX_DEVICES)
2072 * @channel_id: Channel ID number (up to TSPP_NUM_CHANNELS)
2073 * @count: number of buffers to allocate
2074 * @size: size of each buffer to allocate
2075 * @int_freq: interrupt frequency
2076 * @alloc: user defined memory allocator function. Pass NULL for default.
2077 * @memfree: user defined memory free function. Pass NULL for default.
2078 * @user: user data to pass to the memory allocator/free function
2079 *
2080 * Return error status
2081 *
2082 * The user can optionally call this function explicitly to allocate the TSPP
2083 * data buffers. Alternatively, if the user did not call this function, it
2084 * is called implicitly by tspp_add_filter().
2085 */
2086int tspp_allocate_buffers(u32 dev, u32 channel_id, u32 count, u32 size,
2087 u32 int_freq, tspp_allocator *alloc,
2088 tspp_memfree *memfree, void *user)
Joel Nider5bd73f82011-12-14 16:53:30 +02002089{
2090 struct tspp_channel *channel;
2091 struct tspp_device *pdev;
2092 struct tspp_mem_buffer *last = NULL;
2093
2094 TSPP_DEBUG("tspp_allocate_buffers");
2095
2096 if (channel_id >= TSPP_NUM_CHANNELS) {
Liron Kuch229090d2012-10-30 17:47:50 +02002097 pr_err("%s: channel id out of range", __func__);
Joel Nider5bd73f82011-12-14 16:53:30 +02002098 return -ECHRNG;
2099 }
Liron Kuch229090d2012-10-30 17:47:50 +02002100
Joel Nider5bd73f82011-12-14 16:53:30 +02002101 pdev = tspp_find_by_id(dev);
2102 if (!pdev) {
Liron Kuch229090d2012-10-30 17:47:50 +02002103 pr_err("%s: can't find device %i", __func__, dev);
Joel Nider5bd73f82011-12-14 16:53:30 +02002104 return -ENODEV;
2105 }
Liron Kuch229090d2012-10-30 17:47:50 +02002106
2107 if (count < MIN_ACCEPTABLE_BUFFER_COUNT) {
2108 pr_err("%s: tspp requires a minimum of %i buffers\n",
2109 __func__, MIN_ACCEPTABLE_BUFFER_COUNT);
2110 return -EINVAL;
2111 }
2112
Joel Nider5bd73f82011-12-14 16:53:30 +02002113 channel = &pdev->channels[channel_id];
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002114
Liron Kuch229090d2012-10-30 17:47:50 +02002115 /* allow buffer allocation only if there was no previous buffer
2116 * allocation for this channel.
2117 */
2118 if (channel->buffer_count > 0) {
2119 pr_err("%s: buffers already allocated for channel %u",
2120 __func__, channel_id);
2121 return -EINVAL;
2122 }
Joel Nider5bd73f82011-12-14 16:53:30 +02002123
2124 channel->max_buffers = count;
2125
2126 /* set up interrupt frequency */
Liron Kuch229090d2012-10-30 17:47:50 +02002127 if (int_freq > channel->max_buffers) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002128 int_freq = channel->max_buffers;
Liron Kuch229090d2012-10-30 17:47:50 +02002129 pr_warn("%s: setting interrupt frequency to %u\n",
2130 __func__, int_freq);
Joel Nider5bd73f82011-12-14 16:53:30 +02002131 }
Liron Kuch229090d2012-10-30 17:47:50 +02002132 channel->int_freq = int_freq;
2133 /*
2134 * it is the responsibility of the caller to tspp_allocate_buffers(),
2135 * whether it's the user or the driver, to make sure the size parameter
2136 * is compatible to the channel mode.
2137 */
2138 channel->buffer_size = size;
Joel Nider5bd73f82011-12-14 16:53:30 +02002139
Liron Kuch229090d2012-10-30 17:47:50 +02002140 /* save user defined memory free function for later use */
2141 channel->memfree = memfree;
2142 channel->user_info = user;
2143
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002144 /*
2145 * For small buffers, create a DMA pool so that memory
2146 * is not wasted through dma_alloc_coherent.
2147 */
2148 if (TSPP_USE_DMA_POOL(channel->buffer_size)) {
2149 channel->dma_pool = dma_pool_create("tspp",
2150 NULL, channel->buffer_size, 0, 0);
2151 if (!channel->dma_pool) {
2152 pr_err("%s: Can't allocate memory pool\n", __func__);
2153 return -ENOMEM;
2154 }
2155 } else {
2156 channel->dma_pool = NULL;
2157 }
2158
2159
Liron Kuch229090d2012-10-30 17:47:50 +02002160 for (channel->buffer_count = 0;
2161 channel->buffer_count < channel->max_buffers;
Joel Nider5bd73f82011-12-14 16:53:30 +02002162 channel->buffer_count++) {
2163
2164 /* allocate the descriptor */
2165 struct tspp_mem_buffer *desc = (struct tspp_mem_buffer *)
2166 kmalloc(sizeof(struct tspp_mem_buffer), GFP_KERNEL);
2167 if (!desc) {
Liron Kuch229090d2012-10-30 17:47:50 +02002168 pr_warn("%s: Can't allocate desc %i",
2169 __func__, channel->buffer_count);
Joel Nider5bd73f82011-12-14 16:53:30 +02002170 break;
2171 }
2172
2173 desc->desc.id = channel->buffer_count;
2174 /* allocate the buffer */
2175 if (tspp_alloc_buffer(channel_id, &desc->desc,
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002176 channel->buffer_size, channel->dma_pool,
2177 alloc, user) != 0) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002178 kfree(desc);
Liron Kuch229090d2012-10-30 17:47:50 +02002179 pr_warn("%s: Can't allocate buffer %i",
2180 __func__, channel->buffer_count);
Joel Nider5bd73f82011-12-14 16:53:30 +02002181 break;
2182 }
2183
2184 /* add the descriptor to the list */
2185 desc->filled = 0;
2186 desc->read_index = 0;
2187 if (!channel->data) {
2188 channel->data = desc;
2189 desc->next = channel->data;
2190 } else {
2191 last->next = desc;
2192 }
2193 last = desc;
2194 desc->next = channel->data;
2195
2196 /* prepare the sps descriptor */
2197 desc->sps.phys_base = desc->desc.phys_base;
2198 desc->sps.base = desc->desc.virt_base;
2199 desc->sps.size = desc->desc.size;
2200
2201 /* start the transfer */
2202 if (tspp_queue_buffer(channel, desc))
Liron Kuch229090d2012-10-30 17:47:50 +02002203 pr_err("%s: can't queue buffer %i",
2204 __func__, desc->desc.id);
2205 }
2206
2207 if (channel->buffer_count < channel->max_buffers) {
2208 /*
2209 * we failed to allocate the requested number of buffers.
2210 * we don't allow a partial success, so need to clean up here.
2211 */
2212 tspp_destroy_buffers(channel_id, channel);
2213 channel->buffer_count = 0;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002214
2215 if (channel->dma_pool) {
2216 dma_pool_destroy(channel->dma_pool);
2217 channel->dma_pool = NULL;
2218 }
Liron Kuch229090d2012-10-30 17:47:50 +02002219 return -ENOMEM;
Joel Nider5bd73f82011-12-14 16:53:30 +02002220 }
2221
2222 channel->waiting = channel->data;
2223 channel->read = channel->data;
2224 channel->locked = channel->data;
Liron Kuch229090d2012-10-30 17:47:50 +02002225
Hamad Kadmany567bed82012-11-29 14:15:57 +02002226 /* Now that buffers are scheduled to HW, kick data expiration timer */
2227 if (channel->expiration_period_ms)
2228 mod_timer(&channel->expiration_timer,
2229 jiffies +
2230 MSEC_TO_JIFFIES(
2231 channel->expiration_period_ms));
2232
Joel Nider5bd73f82011-12-14 16:53:30 +02002233 return 0;
2234}
2235EXPORT_SYMBOL(tspp_allocate_buffers);
Joel Nider5556a852011-10-16 10:52:13 +02002236
2237/*** File Operations ***/
2238static ssize_t tspp_open(struct inode *inode, struct file *filp)
2239{
Joel Nider5bd73f82011-12-14 16:53:30 +02002240 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002241 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002242
2243 TSPP_DEBUG("tspp_open");
Joel Nider5556a852011-10-16 10:52:13 +02002244 channel = container_of(inode->i_cdev, struct tspp_channel, cdev);
2245 filp->private_data = channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002246 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002247
2248 /* if this channel is already in use, quit */
2249 if (channel->used) {
2250 pr_err("tspp channel %i already in use",
2251 MINOR(channel->cdev.dev));
2252 return -EACCES;
2253 }
2254
Joel Nider5bd73f82011-12-14 16:53:30 +02002255 if (tspp_open_channel(dev, channel->id) != 0) {
Joel Nider5556a852011-10-16 10:52:13 +02002256 pr_err("tspp: error opening channel");
2257 return -EACCES;
2258 }
2259
2260 return 0;
2261}
2262
2263static unsigned int tspp_poll(struct file *filp, struct poll_table_struct *p)
2264{
2265 unsigned long flags;
2266 unsigned int mask = 0;
2267 struct tspp_channel *channel;
2268 channel = filp->private_data;
2269
2270 /* register the wait queue for this channel */
2271 poll_wait(filp, &channel->in_queue, p);
2272
2273 spin_lock_irqsave(&channel->pdev->spinlock, flags);
Joel Nider5bd73f82011-12-14 16:53:30 +02002274 if (channel->read &&
2275 channel->read->state == TSPP_BUF_STATE_DATA)
Joel Nider5556a852011-10-16 10:52:13 +02002276 mask = POLLIN | POLLRDNORM;
2277
2278 spin_unlock_irqrestore(&channel->pdev->spinlock, flags);
2279
2280 return mask;
2281}
2282
2283static ssize_t tspp_release(struct inode *inode, struct file *filp)
2284{
Joel Nider5bd73f82011-12-14 16:53:30 +02002285 struct tspp_channel *channel = filp->private_data;
2286 u32 dev = channel->pdev->pdev->id;
2287 TSPP_DEBUG("tspp_release");
Joel Nider5556a852011-10-16 10:52:13 +02002288
Joel Nider5bd73f82011-12-14 16:53:30 +02002289 tspp_close_channel(dev, channel->id);
Joel Nider5556a852011-10-16 10:52:13 +02002290
2291 return 0;
2292}
2293
2294static ssize_t tspp_read(struct file *filp, char __user *buf, size_t count,
2295 loff_t *f_pos)
2296{
2297 size_t size = 0;
2298 size_t transferred = 0;
2299 struct tspp_channel *channel;
2300 struct tspp_mem_buffer *buffer;
2301 channel = filp->private_data;
2302
2303 TSPP_DEBUG("tspp_read");
Joel Nider5bd73f82011-12-14 16:53:30 +02002304
2305 while (!channel->read) {
2306 if (filp->f_flags & O_NONBLOCK) {
2307 pr_warn("tspp: no buffer on channel %i!",
2308 channel->id);
2309 return -EAGAIN;
2310 }
2311 /* go to sleep if there is nothing to read */
2312 if (wait_event_interruptible(channel->in_queue,
2313 (channel->read != NULL))) {
2314 pr_err("tspp: rude awakening\n");
2315 return -ERESTARTSYS;
2316 }
2317 }
2318
2319 buffer = channel->read;
2320
Joel Nider5556a852011-10-16 10:52:13 +02002321 /* see if we have any buffers ready to read */
2322 while (buffer->state != TSPP_BUF_STATE_DATA) {
2323 if (filp->f_flags & O_NONBLOCK) {
2324 pr_warn("tspp: nothing to read on channel %i!",
2325 channel->id);
2326 return -EAGAIN;
2327 }
2328 /* go to sleep if there is nothing to read */
Joel Nider5556a852011-10-16 10:52:13 +02002329 if (wait_event_interruptible(channel->in_queue,
2330 (buffer->state == TSPP_BUF_STATE_DATA))) {
2331 pr_err("tspp: rude awakening\n");
2332 return -ERESTARTSYS;
2333 }
2334 }
2335
2336 while (buffer->state == TSPP_BUF_STATE_DATA) {
2337 size = min(count, buffer->filled);
Joel Nider5556a852011-10-16 10:52:13 +02002338 if (size == 0)
2339 break;
2340
Joel Nider5bd73f82011-12-14 16:53:30 +02002341 if (copy_to_user(buf, buffer->desc.virt_base +
Joel Nider5556a852011-10-16 10:52:13 +02002342 buffer->read_index, size)) {
2343 pr_err("tspp: error copying to user buffer");
Joel Nider5bd73f82011-12-14 16:53:30 +02002344 return -EBUSY;
Joel Nider5556a852011-10-16 10:52:13 +02002345 }
2346 buf += size;
2347 count -= size;
2348 transferred += size;
2349 buffer->read_index += size;
2350
Liron Kuch229090d2012-10-30 17:47:50 +02002351 /*
2352 * after reading the end of the buffer, requeue it,
2353 * and set up for reading the next one
2354 */
Joel Nider5bd73f82011-12-14 16:53:30 +02002355 if (buffer->read_index == buffer->filled) {
Joel Nider5556a852011-10-16 10:52:13 +02002356 buffer->state = TSPP_BUF_STATE_WAITING;
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002357
Joel Nider5bd73f82011-12-14 16:53:30 +02002358 if (tspp_queue_buffer(channel, buffer))
2359 pr_err("tspp: can't submit transfer");
Hamad Kadmanybb0d0f92013-01-06 12:08:13 +02002360
Joel Nider5bd73f82011-12-14 16:53:30 +02002361 channel->locked = channel->read;
2362 channel->read = channel->read->next;
Joel Nider5556a852011-10-16 10:52:13 +02002363 }
2364 }
2365
2366 return transferred;
2367}
2368
2369static long tspp_ioctl(struct file *filp,
2370 unsigned int param0, unsigned long param1)
2371{
Joel Nider5bd73f82011-12-14 16:53:30 +02002372 u32 dev;
Joel Nider5556a852011-10-16 10:52:13 +02002373 int rc = -1;
2374 struct tspp_channel *channel;
Joel Nider5bd73f82011-12-14 16:53:30 +02002375 struct tspp_select_source ss;
2376 struct tspp_filter f;
2377 struct tspp_key k;
2378 struct tspp_iv iv;
2379 struct tspp_system_keys sk;
2380 struct tspp_buffer b;
Joel Nider5556a852011-10-16 10:52:13 +02002381 channel = filp->private_data;
Joel Nider5bd73f82011-12-14 16:53:30 +02002382 dev = channel->pdev->pdev->id;
Joel Nider5556a852011-10-16 10:52:13 +02002383
2384 if (!param1)
2385 return -EINVAL;
2386
2387 switch (param0) {
2388 case TSPP_IOCTL_SELECT_SOURCE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002389 if (!access_ok(VERIFY_READ, param1,
2390 sizeof(struct tspp_select_source))) {
2391 return -EBUSY;
2392 }
2393 if (__copy_from_user(&ss, (void *)param1,
2394 sizeof(struct tspp_select_source)) == 0)
2395 rc = tspp_select_source(dev, channel->id, &ss);
Joel Nider5556a852011-10-16 10:52:13 +02002396 break;
2397 case TSPP_IOCTL_ADD_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02002398 if (!access_ok(VERIFY_READ, param1,
2399 sizeof(struct tspp_filter))) {
2400 return -ENOSR;
2401 }
2402 if (__copy_from_user(&f, (void *)param1,
2403 sizeof(struct tspp_filter)) == 0)
2404 rc = tspp_add_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002405 break;
2406 case TSPP_IOCTL_REMOVE_FILTER:
Joel Nider5bd73f82011-12-14 16:53:30 +02002407 if (!access_ok(VERIFY_READ, param1,
2408 sizeof(struct tspp_filter))) {
2409 return -EBUSY;
2410 }
2411 if (__copy_from_user(&f, (void *)param1,
2412 sizeof(struct tspp_filter)) == 0)
2413 rc = tspp_remove_filter(dev, channel->id, &f);
Joel Nider5556a852011-10-16 10:52:13 +02002414 break;
2415 case TSPP_IOCTL_SET_KEY:
Joel Nider5bd73f82011-12-14 16:53:30 +02002416 if (!access_ok(VERIFY_READ, param1,
2417 sizeof(struct tspp_key))) {
2418 return -EBUSY;
2419 }
2420 if (__copy_from_user(&k, (void *)param1,
2421 sizeof(struct tspp_key)) == 0)
2422 rc = tspp_set_key(dev, channel->id, &k);
Joel Nider5556a852011-10-16 10:52:13 +02002423 break;
2424 case TSPP_IOCTL_SET_IV:
Joel Nider5bd73f82011-12-14 16:53:30 +02002425 if (!access_ok(VERIFY_READ, param1,
2426 sizeof(struct tspp_iv))) {
2427 return -EBUSY;
2428 }
2429 if (__copy_from_user(&iv, (void *)param1,
2430 sizeof(struct tspp_iv)) == 0)
2431 rc = tspp_set_iv(channel, &iv);
Joel Nider5556a852011-10-16 10:52:13 +02002432 break;
2433 case TSPP_IOCTL_SET_SYSTEM_KEYS:
Joel Nider5bd73f82011-12-14 16:53:30 +02002434 if (!access_ok(VERIFY_READ, param1,
2435 sizeof(struct tspp_system_keys))) {
2436 return -EINVAL;
2437 }
2438 if (__copy_from_user(&sk, (void *)param1,
2439 sizeof(struct tspp_system_keys)) == 0)
2440 rc = tspp_set_system_keys(channel, &sk);
Joel Nider5556a852011-10-16 10:52:13 +02002441 break;
2442 case TSPP_IOCTL_BUFFER_SIZE:
Joel Nider5bd73f82011-12-14 16:53:30 +02002443 if (!access_ok(VERIFY_READ, param1,
2444 sizeof(struct tspp_buffer))) {
2445 rc = -EINVAL;
2446 }
2447 if (__copy_from_user(&b, (void *)param1,
2448 sizeof(struct tspp_buffer)) == 0)
2449 rc = tspp_set_buffer_size(channel, &b);
Joel Nider5556a852011-10-16 10:52:13 +02002450 break;
2451 default:
2452 pr_err("tspp: Unknown ioctl %i", param0);
2453 }
2454
Liron Kuch229090d2012-10-30 17:47:50 +02002455 /*
2456 * normalize the return code in case one of the subfunctions does
2457 * something weird
2458 */
Joel Nider5556a852011-10-16 10:52:13 +02002459 if (rc != 0)
Joel Nider5bd73f82011-12-14 16:53:30 +02002460 rc = -ENOIOCTLCMD;
Joel Nider5556a852011-10-16 10:52:13 +02002461
2462 return rc;
2463}
2464
2465/*** debugfs ***/
Joel Nider5556a852011-10-16 10:52:13 +02002466static int debugfs_iomem_x32_set(void *data, u64 val)
2467{
2468 writel_relaxed(val, data);
2469 wmb();
2470 return 0;
2471}
2472
2473static int debugfs_iomem_x32_get(void *data, u64 *val)
2474{
2475 *val = readl_relaxed(data);
2476 return 0;
2477}
2478
2479DEFINE_SIMPLE_ATTRIBUTE(fops_iomem_x32, debugfs_iomem_x32_get,
2480 debugfs_iomem_x32_set, "0x%08llx");
2481
2482static void tsif_debugfs_init(struct tspp_tsif_device *tsif_device,
2483 int instance)
2484{
2485 char name[10];
2486 snprintf(name, 10, "tsif%i", instance);
2487 tsif_device->dent_tsif = debugfs_create_dir(
2488 name, NULL);
2489 if (tsif_device->dent_tsif) {
2490 int i;
2491 void __iomem *base = tsif_device->base;
2492 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++) {
2493 tsif_device->debugfs_tsif_regs[i] =
2494 debugfs_create_file(
2495 debugfs_tsif_regs[i].name,
2496 debugfs_tsif_regs[i].mode,
2497 tsif_device->dent_tsif,
2498 base + debugfs_tsif_regs[i].offset,
2499 &fops_iomem_x32);
2500 }
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002501
2502 debugfs_create_u32(
2503 "stat_rx_chunks",
2504 S_IRUGO|S_IWUGO,
2505 tsif_device->dent_tsif,
2506 &tsif_device->stat_rx);
2507
2508 debugfs_create_u32(
2509 "stat_overflow",
2510 S_IRUGO|S_IWUGO,
2511 tsif_device->dent_tsif,
2512 &tsif_device->stat_overflow);
2513
2514 debugfs_create_u32(
2515 "stat_lost_sync",
2516 S_IRUGO|S_IWUGO,
2517 tsif_device->dent_tsif,
2518 &tsif_device->stat_lost_sync);
2519
2520 debugfs_create_u32(
2521 "stat_timeout",
2522 S_IRUGO|S_IWUGO,
2523 tsif_device->dent_tsif,
2524 &tsif_device->stat_timeout);
2525
Joel Nider5556a852011-10-16 10:52:13 +02002526 }
2527}
2528
2529static void tsif_debugfs_exit(struct tspp_tsif_device *tsif_device)
2530{
2531 if (tsif_device->dent_tsif) {
2532 int i;
2533 debugfs_remove_recursive(tsif_device->dent_tsif);
2534 tsif_device->dent_tsif = NULL;
2535 for (i = 0; i < ARRAY_SIZE(debugfs_tsif_regs); i++)
2536 tsif_device->debugfs_tsif_regs[i] = NULL;
2537 }
2538}
2539
2540static void tspp_debugfs_init(struct tspp_device *device, int instance)
2541{
2542 char name[10];
2543 snprintf(name, 10, "tspp%i", instance);
2544 device->dent = debugfs_create_dir(
2545 name, NULL);
2546 if (device->dent) {
2547 int i;
2548 void __iomem *base = device->base;
2549 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++) {
2550 device->debugfs_regs[i] =
2551 debugfs_create_file(
2552 debugfs_tspp_regs[i].name,
2553 debugfs_tspp_regs[i].mode,
2554 device->dent,
2555 base + debugfs_tspp_regs[i].offset,
2556 &fops_iomem_x32);
2557 }
2558 }
2559}
2560
2561static void tspp_debugfs_exit(struct tspp_device *device)
2562{
2563 if (device->dent) {
2564 int i;
2565 debugfs_remove_recursive(device->dent);
2566 device->dent = NULL;
2567 for (i = 0; i < ARRAY_SIZE(debugfs_tspp_regs); i++)
2568 device->debugfs_regs[i] = NULL;
2569 }
2570}
Joel Nider5556a852011-10-16 10:52:13 +02002571
Liron Kuch8fa85b02013-01-01 18:29:47 +02002572/* copy device-tree data to platfrom data struct */
2573static __devinit struct msm_tspp_platform_data *
2574msm_tspp_dt_to_pdata(struct platform_device *pdev)
2575{
2576 struct device_node *node = pdev->dev.of_node;
2577 struct msm_tspp_platform_data *data;
2578 struct msm_gpio *gpios;
2579 int i, rc;
2580 int gpio;
2581 u32 gpio_func;
2582
2583 /* Note: memory allocated by devm_kzalloc is freed automatically */
2584 data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL);
2585 if (!data) {
2586 pr_err("tspp: Unable to allocate platform data\n");
2587 return NULL;
2588 }
2589 rc = of_property_read_string(node, "qcom,tsif-pclk", &data->tsif_pclk);
2590 if (rc) {
2591 pr_err("tspp: Could not find tsif-pclk property, err = %d\n",
2592 rc);
2593 return NULL;
2594 }
2595 rc = of_property_read_string(node, "qcom,tsif-ref-clk",
2596 &data->tsif_ref_clk);
2597 if (rc) {
2598 pr_err("tspp: Could not find tsif-ref-clk property, err = %d\n",
2599 rc);
2600 return NULL;
2601 }
2602
2603 data->num_gpios = of_gpio_count(node);
2604 if (data->num_gpios == 0) {
2605 pr_err("tspp: Could not find GPIO definitions\n");
2606 return NULL;
2607 }
2608 gpios = devm_kzalloc(&pdev->dev,
2609 (data->num_gpios * sizeof(struct msm_gpio)),
2610 GFP_KERNEL);
2611 if (!gpios) {
2612 pr_err("tspp: Unable to allocate memory for GPIOs table\n");
2613 return NULL;
2614 }
2615 /* Assuming GPIO FUNC property is the same for all GPIOs */
2616 if (of_property_read_u32(node, "qcom,gpios-func", &gpio_func)) {
2617 pr_err("tspp: Could not find gpios-func property\n");
2618 return NULL;
2619 }
2620 for (i = 0; i < data->num_gpios; i++) {
2621 gpio = of_get_gpio(node, i);
2622 gpios[i].gpio_cfg = GPIO_CFG(gpio, gpio_func,
2623 GPIO_CFG_INPUT,
2624 GPIO_CFG_PULL_DOWN,
2625 GPIO_CFG_2MA);
2626 rc = of_property_read_string_index(node, "qcom,gpio-names",
2627 i, &gpios[i].label);
2628 if (rc)
2629 pr_warn("tspp: Could not find gpio-names property\n");
2630 }
2631
2632 data->gpios = gpios;
2633
2634 return data;
2635}
2636
2637static int msm_tspp_map_irqs(struct platform_device *pdev,
2638 struct tspp_device *device)
2639{
2640 int rc;
2641 int i;
2642
2643 /* get IRQ numbers from platform information */
2644
2645 /* map TSPP IRQ */
2646 rc = platform_get_irq_byname(pdev, "TSIF_TSPP_IRQ");
2647 if (rc > 0) {
2648 device->tspp_irq = rc;
2649 rc = request_irq(device->tspp_irq, tspp_isr, IRQF_SHARED,
2650 dev_name(&pdev->dev), device);
2651 if (rc) {
2652 dev_err(&pdev->dev,
2653 "failed to request TSPP IRQ %d : %d",
2654 device->tspp_irq, rc);
2655 device->tspp_irq = 0;
2656 return -EINVAL;
2657 }
2658 } else {
2659 dev_err(&pdev->dev, "failed to get TSPP IRQ");
2660 return -EINVAL;
2661 }
2662
2663 /* map TSIF IRQs */
2664 rc = platform_get_irq_byname(pdev, "TSIF0_IRQ");
2665 if (rc > 0) {
2666 device->tsif[0].tsif_irq = rc;
2667 } else {
2668 dev_err(&pdev->dev, "failed to get TSIF0 IRQ");
2669 return -EINVAL;
2670 }
2671
2672 rc = platform_get_irq_byname(pdev, "TSIF1_IRQ");
2673 if (rc > 0) {
2674 device->tsif[1].tsif_irq = rc;
2675 } else {
2676 dev_err(&pdev->dev, "failed to get TSIF1 IRQ");
2677 return -EINVAL;
2678 }
2679
2680 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2681 rc = request_irq(device->tsif[i].tsif_irq,
2682 tsif_isr, IRQF_SHARED,
2683 dev_name(&pdev->dev), &device->tsif[i]);
2684 if (rc) {
2685 dev_warn(&pdev->dev, "failed to request TSIF%d IRQ: %d",
2686 i, rc);
2687 device->tsif[i].tsif_irq = 0;
2688 }
2689 }
2690
2691 /* map BAM IRQ */
2692 rc = platform_get_irq_byname(pdev, "TSIF_BAM_IRQ");
2693 if (rc > 0) {
2694 device->bam_irq = rc;
2695 } else {
2696 dev_err(&pdev->dev, "failed to get TSPP BAM IRQ");
2697 return -EINVAL;
2698 }
2699
2700 return 0;
2701}
2702
Joel Nider5556a852011-10-16 10:52:13 +02002703static int __devinit msm_tspp_probe(struct platform_device *pdev)
2704{
2705 int rc = -ENODEV;
2706 u32 version;
Liron Kuch229090d2012-10-30 17:47:50 +02002707 u32 i, j;
Joel Nider5556a852011-10-16 10:52:13 +02002708 struct msm_tspp_platform_data *data;
2709 struct tspp_device *device;
2710 struct resource *mem_tsif0;
2711 struct resource *mem_tsif1;
2712 struct resource *mem_tspp;
2713 struct resource *mem_bam;
Liron Kuch229090d2012-10-30 17:47:50 +02002714 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002715
Liron Kuch8fa85b02013-01-01 18:29:47 +02002716 if (pdev->dev.of_node) {
2717 /* get information from device tree */
2718 data = msm_tspp_dt_to_pdata(pdev);
2719 /* get device ID */
2720 rc = of_property_read_u32(pdev->dev.of_node,
2721 "cell-index", &pdev->id);
2722 if (rc)
2723 pdev->id = -1;
2724
2725 pdev->dev.platform_data = data;
2726 } else {
2727 /* must have platform data */
2728 data = pdev->dev.platform_data;
2729 }
Joel Nider5556a852011-10-16 10:52:13 +02002730 if (!data) {
2731 pr_err("tspp: Platform data not available");
2732 rc = -EINVAL;
2733 goto out;
2734 }
2735
2736 /* check for valid device id */
Joel Nider5bd73f82011-12-14 16:53:30 +02002737 if ((pdev->id < 0) || (pdev->id >= TSPP_MAX_DEVICES)) {
Joel Nider5556a852011-10-16 10:52:13 +02002738 pr_err("tspp: Invalid device ID %d", pdev->id);
2739 rc = -EINVAL;
2740 goto out;
2741 }
2742
2743 /* OK, we will use this device */
2744 device = kzalloc(sizeof(struct tspp_device), GFP_KERNEL);
2745 if (!device) {
2746 pr_err("tspp: Failed to allocate memory for device");
2747 rc = -ENOMEM;
2748 goto out;
2749 }
2750
2751 /* set up references */
2752 device->pdev = pdev;
2753 platform_set_drvdata(pdev, device);
2754
2755 /* map clocks */
2756 if (data->tsif_pclk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002757 device->tsif_pclk = clk_get(&pdev->dev, data->tsif_pclk);
Joel Nider5556a852011-10-16 10:52:13 +02002758 if (IS_ERR(device->tsif_pclk)) {
2759 pr_err("tspp: failed to get %s",
2760 data->tsif_pclk);
2761 rc = PTR_ERR(device->tsif_pclk);
2762 device->tsif_pclk = NULL;
2763 goto err_pclock;
2764 }
2765 }
2766 if (data->tsif_ref_clk) {
Joel Niderb9662ca2012-06-10 14:21:11 +03002767 device->tsif_ref_clk = clk_get(&pdev->dev, data->tsif_ref_clk);
Joel Nider5556a852011-10-16 10:52:13 +02002768 if (IS_ERR(device->tsif_ref_clk)) {
2769 pr_err("tspp: failed to get %s",
2770 data->tsif_ref_clk);
2771 rc = PTR_ERR(device->tsif_ref_clk);
2772 device->tsif_ref_clk = NULL;
2773 goto err_refclock;
2774 }
2775 }
2776
2777 /* map I/O memory */
Liron Kuch8fa85b02013-01-01 18:29:47 +02002778 mem_tsif0 = platform_get_resource_byname(pdev,
2779 IORESOURCE_MEM, "MSM_TSIF0_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002780 if (!mem_tsif0) {
2781 pr_err("tspp: Missing tsif0 MEM resource");
2782 rc = -ENXIO;
2783 goto err_res_tsif0;
2784 }
2785 device->tsif[0].base = ioremap(mem_tsif0->start,
2786 resource_size(mem_tsif0));
2787 if (!device->tsif[0].base) {
2788 pr_err("tspp: ioremap failed");
2789 goto err_map_tsif0;
2790 }
2791
Liron Kuch8fa85b02013-01-01 18:29:47 +02002792 mem_tsif1 = platform_get_resource_byname(pdev,
2793 IORESOURCE_MEM, "MSM_TSIF1_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002794 if (!mem_tsif1) {
2795 dev_err(&pdev->dev, "Missing tsif1 MEM resource");
2796 rc = -ENXIO;
2797 goto err_res_tsif1;
2798 }
2799 device->tsif[1].base = ioremap(mem_tsif1->start,
2800 resource_size(mem_tsif1));
2801 if (!device->tsif[1].base) {
2802 dev_err(&pdev->dev, "ioremap failed");
2803 goto err_map_tsif1;
2804 }
2805
Liron Kuch8fa85b02013-01-01 18:29:47 +02002806 mem_tspp = platform_get_resource_byname(pdev,
2807 IORESOURCE_MEM, "MSM_TSPP_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002808 if (!mem_tspp) {
2809 dev_err(&pdev->dev, "Missing MEM resource");
2810 rc = -ENXIO;
2811 goto err_res_dev;
2812 }
2813 device->base = ioremap(mem_tspp->start, resource_size(mem_tspp));
2814 if (!device->base) {
2815 dev_err(&pdev->dev, "ioremap failed");
2816 goto err_map_dev;
2817 }
2818
Liron Kuch8fa85b02013-01-01 18:29:47 +02002819 mem_bam = platform_get_resource_byname(pdev,
2820 IORESOURCE_MEM, "MSM_TSPP_BAM_PHYS");
Joel Nider5556a852011-10-16 10:52:13 +02002821 if (!mem_bam) {
2822 pr_err("tspp: Missing bam MEM resource");
2823 rc = -ENXIO;
2824 goto err_res_bam;
2825 }
2826 memset(&device->bam_props, 0, sizeof(device->bam_props));
2827 device->bam_props.phys_addr = mem_bam->start;
2828 device->bam_props.virt_addr = ioremap(mem_bam->start,
2829 resource_size(mem_bam));
2830 if (!device->bam_props.virt_addr) {
2831 dev_err(&pdev->dev, "ioremap failed");
2832 goto err_map_bam;
2833 }
2834
Liron Kuch8fa85b02013-01-01 18:29:47 +02002835 if (msm_tspp_map_irqs(pdev, device))
Joel Nider5556a852011-10-16 10:52:13 +02002836 goto err_irq;
Joel Nider5556a852011-10-16 10:52:13 +02002837
2838 /* GPIOs */
2839 rc = tspp_start_gpios(device);
2840 if (rc)
2841 goto err_gpio;
2842
2843 /* power management */
2844 pm_runtime_set_active(&pdev->dev);
2845 pm_runtime_enable(&pdev->dev);
2846
Joel Nider5556a852011-10-16 10:52:13 +02002847 tspp_debugfs_init(device, 0);
2848
2849 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2850 tsif_debugfs_init(&device->tsif[i], i);
Joel Nider5556a852011-10-16 10:52:13 +02002851
2852 wake_lock_init(&device->wake_lock, WAKE_LOCK_SUSPEND,
2853 dev_name(&pdev->dev));
2854
2855 /* set up pointers to ram-based 'registers' */
Joel Nider5bd73f82011-12-14 16:53:30 +02002856 device->filters[0] = device->base + TSPP_PID_FILTER_TABLE0;
2857 device->filters[1] = device->base + TSPP_PID_FILTER_TABLE1;
2858 device->filters[2] = device->base + TSPP_PID_FILTER_TABLE2;
2859 device->tspp_key_table = device->base + TSPP_DATA_KEY;
2860 device->tspp_global_performance =
2861 device->base + TSPP_GLOBAL_PERFORMANCE;
2862 device->tspp_pipe_context =
2863 device->base + TSPP_PIPE_CONTEXT;
2864 device->tspp_pipe_performance =
2865 device->base + TSPP_PIPE_PERFORMANCE;
Joel Nider5556a852011-10-16 10:52:13 +02002866
2867 device->bam_props.summing_threshold = 0x10;
2868 device->bam_props.irq = device->bam_irq;
2869 device->bam_props.manage = SPS_BAM_MGR_LOCAL;
2870
Liron Kuch8fa85b02013-01-01 18:29:47 +02002871 if (tspp_clock_start(device) != 0) {
2872 dev_err(&pdev->dev, "Can't start clocks");
2873 goto err_clock;
2874 }
2875
Joel Nider5556a852011-10-16 10:52:13 +02002876 if (sps_register_bam_device(&device->bam_props,
2877 &device->bam_handle) != 0) {
2878 pr_err("tspp: failed to register bam");
2879 goto err_bam;
2880 }
2881
Joel Nider5556a852011-10-16 10:52:13 +02002882 spin_lock_init(&device->spinlock);
2883 tasklet_init(&device->tlet, tspp_sps_complete_tlet,
2884 (unsigned long)device);
2885
2886 /* initialize everything to a known state */
2887 tspp_global_reset(device);
2888
2889 version = readl_relaxed(device->base + TSPP_VERSION);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002890 /*
2891 * TSPP version can be bits [7:0] or alternatively,
2892 * TSPP major version is bits [31:28].
2893 */
2894 if ((version != 0x1) && (((version >> 28) & 0xF) != 0x1))
Joel Nider5556a852011-10-16 10:52:13 +02002895 pr_warn("tspp: unrecognized hw version=%i", version);
2896
Joel Nider5bd73f82011-12-14 16:53:30 +02002897 /* initialize the channels */
Joel Nider5556a852011-10-16 10:52:13 +02002898 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
Joel Nider5bd73f82011-12-14 16:53:30 +02002899 if (tspp_channel_init(&(device->channels[i]), device) != 0) {
2900 pr_err("tspp_channel_init failed");
2901 goto err_channel;
2902 }
Joel Nider5556a852011-10-16 10:52:13 +02002903 }
2904
Joel Nider5bd73f82011-12-14 16:53:30 +02002905 /* stop the clocks for power savings */
2906 tspp_clock_stop(device);
2907
2908 /* everything is ok, so add the device to the list */
2909 list_add_tail(&(device->devlist), &tspp_devices);
2910
Joel Nider5556a852011-10-16 10:52:13 +02002911 return 0;
2912
Joel Nider5bd73f82011-12-14 16:53:30 +02002913err_channel:
Liron Kuch8fa85b02013-01-01 18:29:47 +02002914 /* un-initialize channels */
Liron Kuch229090d2012-10-30 17:47:50 +02002915 for (j = 0; j < i; j++) {
2916 channel = &(device->channels[i]);
2917 device_destroy(tspp_class, channel->cdev.dev);
2918 cdev_del(&channel->cdev);
2919 }
Liron Kuch8fa85b02013-01-01 18:29:47 +02002920
Joel Nider5556a852011-10-16 10:52:13 +02002921 sps_deregister_bam_device(device->bam_handle);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002922err_clock:
Joel Nider5556a852011-10-16 10:52:13 +02002923err_bam:
Joel Nider5556a852011-10-16 10:52:13 +02002924 tspp_debugfs_exit(device);
2925 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2926 tsif_debugfs_exit(&device->tsif[i]);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002927
2928 tspp_stop_gpios(device);
Joel Nider5556a852011-10-16 10:52:13 +02002929err_gpio:
2930err_irq:
Liron Kuch8fa85b02013-01-01 18:29:47 +02002931 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
2932 if (device->tsif[i].tsif_irq)
2933 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
2934 }
2935 if (device->tspp_irq)
2936 free_irq(device->tspp_irq, device);
2937
Joel Nider5556a852011-10-16 10:52:13 +02002938 iounmap(device->bam_props.virt_addr);
2939err_map_bam:
2940err_res_bam:
2941 iounmap(device->base);
2942err_map_dev:
2943err_res_dev:
2944 iounmap(device->tsif[1].base);
2945err_map_tsif1:
2946err_res_tsif1:
2947 iounmap(device->tsif[0].base);
2948err_map_tsif0:
2949err_res_tsif0:
2950 if (device->tsif_ref_clk)
2951 clk_put(device->tsif_ref_clk);
2952err_refclock:
2953 if (device->tsif_pclk)
2954 clk_put(device->tsif_pclk);
2955err_pclock:
2956 kfree(device);
2957
2958out:
2959 return rc;
2960}
2961
2962static int __devexit msm_tspp_remove(struct platform_device *pdev)
2963{
Joel Nider5bd73f82011-12-14 16:53:30 +02002964 struct tspp_channel *channel;
Joel Nider5556a852011-10-16 10:52:13 +02002965 u32 i;
Joel Nider5556a852011-10-16 10:52:13 +02002966
2967 struct tspp_device *device = platform_get_drvdata(pdev);
2968
Joel Nider5bd73f82011-12-14 16:53:30 +02002969 /* free the buffers, and delete the channels */
2970 for (i = 0; i < TSPP_NUM_CHANNELS; i++) {
2971 channel = &device->channels[i];
2972 tspp_close_channel(device->pdev->id, i);
2973 device_destroy(tspp_class, channel->cdev.dev);
2974 cdev_del(&channel->cdev);
2975 }
2976
Liron Kuch8fa85b02013-01-01 18:29:47 +02002977 /* de-registering BAM device requires clocks */
2978 tspp_clock_start(device);
Joel Nider5556a852011-10-16 10:52:13 +02002979 sps_deregister_bam_device(device->bam_handle);
Liron Kuch8fa85b02013-01-01 18:29:47 +02002980 tspp_clock_stop(device);
Joel Nider5556a852011-10-16 10:52:13 +02002981
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002982 for (i = 0; i < TSPP_TSIF_INSTANCES; i++) {
Joel Nider5556a852011-10-16 10:52:13 +02002983 tsif_debugfs_exit(&device->tsif[i]);
Hamad Kadmanyf0cc2712012-11-25 09:49:51 +02002984 if (device->tsif[i].tsif_irq)
2985 free_irq(device->tsif[i].tsif_irq, &device->tsif[i]);
2986 }
Joel Nider5556a852011-10-16 10:52:13 +02002987
2988 wake_lock_destroy(&device->wake_lock);
2989 free_irq(device->tspp_irq, device);
2990 tspp_stop_gpios(device);
2991
2992 iounmap(device->bam_props.virt_addr);
2993 iounmap(device->base);
2994 for (i = 0; i < TSPP_TSIF_INSTANCES; i++)
2995 iounmap(device->tsif[i].base);
2996
2997 if (device->tsif_ref_clk)
2998 clk_put(device->tsif_ref_clk);
2999
3000 if (device->tsif_pclk)
3001 clk_put(device->tsif_pclk);
3002
3003 pm_runtime_disable(&pdev->dev);
3004 pm_runtime_put(&pdev->dev);
3005 kfree(device);
3006
3007 return 0;
3008}
3009
3010/*** power management ***/
3011
3012static int tspp_runtime_suspend(struct device *dev)
3013{
3014 dev_dbg(dev, "pm_runtime: suspending...");
3015 return 0;
3016}
3017
3018static int tspp_runtime_resume(struct device *dev)
3019{
3020 dev_dbg(dev, "pm_runtime: resuming...");
3021 return 0;
3022}
3023
3024static const struct dev_pm_ops tspp_dev_pm_ops = {
3025 .runtime_suspend = tspp_runtime_suspend,
3026 .runtime_resume = tspp_runtime_resume,
3027};
3028
Liron Kuch8fa85b02013-01-01 18:29:47 +02003029static struct of_device_id msm_match_table[] = {
3030 {.compatible = "qcom,msm_tspp"},
3031 {}
3032};
3033
Joel Nider5556a852011-10-16 10:52:13 +02003034static struct platform_driver msm_tspp_driver = {
3035 .probe = msm_tspp_probe,
3036 .remove = __exit_p(msm_tspp_remove),
3037 .driver = {
3038 .name = "msm_tspp",
3039 .pm = &tspp_dev_pm_ops,
Liron Kuch8fa85b02013-01-01 18:29:47 +02003040 .of_match_table = msm_match_table,
Joel Nider5556a852011-10-16 10:52:13 +02003041 },
3042};
3043
3044
3045static int __init mod_init(void)
3046{
Joel Nider5556a852011-10-16 10:52:13 +02003047 int rc;
3048
Joel Nider5bd73f82011-12-14 16:53:30 +02003049 /* make the char devs (channels) */
Joel Nider5556a852011-10-16 10:52:13 +02003050 rc = alloc_chrdev_region(&tspp_minor, 0, TSPP_NUM_CHANNELS, "tspp");
3051 if (rc) {
3052 pr_err("tspp: alloc_chrdev_region failed: %d", rc);
3053 goto err_devrgn;
3054 }
3055
3056 tspp_class = class_create(THIS_MODULE, "tspp");
3057 if (IS_ERR(tspp_class)) {
3058 rc = PTR_ERR(tspp_class);
3059 pr_err("tspp: Error creating class: %d", rc);
3060 goto err_class;
3061 }
3062
Joel Nider5bd73f82011-12-14 16:53:30 +02003063 /* register the driver, and check hardware */
3064 rc = platform_driver_register(&msm_tspp_driver);
3065 if (rc) {
3066 pr_err("tspp: platform_driver_register failed: %d", rc);
3067 goto err_register;
Joel Nider5556a852011-10-16 10:52:13 +02003068 }
3069
3070 return 0;
3071
Joel Nider5bd73f82011-12-14 16:53:30 +02003072err_register:
3073 class_destroy(tspp_class);
Joel Nider5556a852011-10-16 10:52:13 +02003074err_class:
3075 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
3076err_devrgn:
Joel Nider5556a852011-10-16 10:52:13 +02003077 return rc;
3078}
3079
3080static void __exit mod_exit(void)
3081{
Joel Nider5bd73f82011-12-14 16:53:30 +02003082 /* delete low level driver */
3083 platform_driver_unregister(&msm_tspp_driver);
Joel Nider5556a852011-10-16 10:52:13 +02003084
Joel Nider5bd73f82011-12-14 16:53:30 +02003085 /* delete upper layer interface */
Joel Nider5556a852011-10-16 10:52:13 +02003086 class_destroy(tspp_class);
3087 unregister_chrdev_region(0, TSPP_NUM_CHANNELS);
Joel Nider5556a852011-10-16 10:52:13 +02003088}
3089
3090module_init(mod_init);
3091module_exit(mod_exit);
3092
Joel Nider5bd73f82011-12-14 16:53:30 +02003093MODULE_DESCRIPTION("TSPP platform device and char dev");
Joel Nider5556a852011-10-16 10:52:13 +02003094MODULE_LICENSE("GPL v2");